您的位置首页  散文评论

redeem code(redeem code is invalid)墙裂推荐

对于IOS大家都不陌生,是由苹果公司开发的一款优秀的移动操作系统,考虑到我们最近正在学习Web Service相关知识,那么如何写Web Ser

redeem code(redeem code is invalid)墙裂推荐

 

对于IOS大家都不陌生,是由苹果公司开发的一款优秀的移动操作系统,考虑到我们最近正在学习Web Service相关知识,那么如何写Web Service的IOS应用呢?

下面我们为大家一步一步来展示Web Service的IOS应用的具体步骤:一、 前期准备工作:下载需要使用的第三方库:JSON,ASIHTTPRequest和MBProgressHUD接下来就开始创建项目并导入:

1.开启Xcode创建一个项目,项目类型选择Single View Application2.创建三个Group,并导入上述三个库JSON:将JSON\Classes目录的文件托入刚才创建的JSON GROUP。

ASIHTTPRequest:将ASIHTTPRequest\Classes目录的所有文件拖入创建的ASIHTTPRequest GROUP(注意,只要当前目录的文件,CloudFiles之类的目录不需要)

ASIHTTPRequest\External\Reachability这里的文件也要加进来MBProgressHUD:将MBProgressHUD.m和MBProgressHUD.h拖入MBProgressHUD GROUP

以上三个操作,拖入的时候,记得勾选Copy items into destination group’s folder (if needed)选项,意思是把目录复制到你的项目中,而不是只引用3.导入一些必要的frameworks:点击左侧导航栏中你的项目->选中target->再选择build phases栏0->Link Binary with Libraries。

点击+按钮,搜索CFNetwork.framework and SystemConfiguration.framework,MobileCoreServices.framework, and libz.1.2.3.dylib四个库。

以上三个大步骤完成后,点击编译完成第一个阶段二、实现Interface创建UI:1.label2.textfield3.textview三、与WebService交互我们的Web Service需要三个参数:。

rw_app_id: 应用的唯一标识号. If you’ve been following along with the previous tutorial, there should be only one entry so far, App ID #1.

code: The code to attempt to redeem. This should be a string that’s entered by the user.device_id: The device ID that is attempting to redeem this code. We can get this with an easy API call

我们需要使用POST机制请求WebServiceASIHTTPRequest将使这一过程变得很便捷1.创建一个ASIFormDataRequest实例与URL2.使用setPostValue方法指定各个参数。

3.设置viewcontroller为request的delegate,之后调用startAsynchronous来发起异步请求4.当请求完毕后,requestFinished或者requestFailed会被回调

5.requestFinished无论webservice相应一个错误的代码,或者正确响应,都会被调用,所以在这个函数里要检查请求成功或者失败6.如果一切顺利,再解析收到的JSON数据- (BOOL)textFieldShouldReturn:(UITextField *)textField {

NSLog(@"Want to redeem: %@", textField.text);// Get device unique IDUIDevice *device = [UIDevice currentDevice];

NSString *uniqueId= [device uniqueIdentifier];// Start requestNSString *code = textField.text;NSURL *url = [NSURL URLWithString:@"http://www.wildfables.com/promos/"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];[request setPostValue:@"1" forKey:@"rw_app_id"];

[request setPostValue:code forKey:@"code"];[request setPostValue:uniqueId forKey:@"device_id"];[request setDelegate:self];

[request startAsynchronous];// Hide keyword[textField resignFirstResponder];// Clear text fieldtextView.text = @"";

//状态指示器MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];hud.labelText = @"Redeeming code...";

return TRUE;}/*请求完成后回调*/- (void)requestFinished:(ASIHTTPRequest *)request{[MBProgressHUD hideHUDForView:self.view animated:YES];

if (request.responseStatusCode == 400) {textView.text = @"Invalid code";} else if (request.responseStatusCode == 403) {

textView.text = @"Code already used";} else if (request.responseStatusCode == 200) {NSString *responseString = [request responseString];

NSDictionary *responseDict = [responseString JSONValue];NSString *unlockCode = [responseDict objectForKey:@"unlock_code"];

if ([unlockCode compare:@"com.razeware.test.unlock.cake"] == NSOrderedSame) {textView.text = @"The cake is a lie!";

} else {textView.text = [NSString stringWithFormat:@"Received unexpected unlock code: %@", unlockCode];

}} else {textView.text = @"Unexpected error";}}/*请求失败后回调*/- (void)requestFailed:(ASIHTTPRequest *)request

{[MBProgressHUD hideHUDForView:self.view animated:YES];NSError *error = [request error];textView.text = error.localizedDescription;

}为了让用户感受到,在请求数据的时候,程序在运行,而不是假死,所以要添加状态指示器三个步骤// Add at the top of the file#import "MBProgressHUD.h"// Add right before return TRUE in textFieldShouldReturn

MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];hud.labelText =@"Redeeming code...";

// Add at start of requestFinished AND requestFailed[MBProgressHUD hideHUDForView:self.view animated:YES];

编译运行,大功告成一个使用Web Service的IOS应用就编写成功啦,当然真正的一款合格的IOS应用还需要精心雕琢,查漏补缺为了能够胜任这些任务,建议小伙伴们观看本站的Java教程,学习更多的优质知识来丰富自己,提升自己的编程水平。

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186