ios处理网络请求,用的比较多的是第三方工具类AFNetworking,因为原生的比较麻烦。
如登录时发送账号跟密码跟后台比对,过程如下,后台返回的是json,如
{"login":{"id":"0001","status":"OK","errorMsg":""}}
下面是使用时的举例。
1 #import "AFNetWorking.h" 2 3 NSString urlLogin = @"www.xxx.com"; 4 //创建请求管理器 5 AFHTTPRequestOperationManager *requestManager = [[AFHTTPRequestOperationManager alloc] init]; 6 //申明请求的数据为text/html型 7 requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil]; 8 requestManager.requestSerializer.HTTPShouldHandleCookies = YES; 9 [requestManager GET:urlLogin 10 parameters:nil 11 success:^(AFHTTPRequestOperation *operation, id responseObject) { 12 NSDictionary *dict = responseObject[@"login"]; 13 NSString *str = [dict objectForKey:@"status"]; 14 NSString *errorMsg = [dict objectForKey:@"errorMsg"]; 15 //如果比对成功,就跳往下个页面 16 if (str && [str isEqualToString:@"OK"]) { 17 MSTabBarViewController *tabBar = [[MSTabBarViewController alloc] init]; 18 [self presentViewController:tabBar animated:YES completion:nil]; 19 } else { 20 //比对不成功,弹出提示 21 self.alertView = [[UIAlertView alloc]initWithTitle:@"" message:errorMsg delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok", nil]; 22 [self.alertView show]; 23 } 24 25 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 26 NSLog(@"服务器出错"); 27 }];
当然前提是得引入AFNetWorking的文件。
时间: 2024-10-07 23:28:04