用户登录时app常用的,需要将用户名和密码以及一些服务端需要的数据传给服务端,代码示例如下
- (void)login { NSLog(@"login btn has pressed! username = %@, password=%@", _username.text, _password.text); _hud = [Utils createHUD]; _hud.labelText = @"正在登录"; _hud.userInteractionEnabled = NO; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:[NSString stringWithFormat:@"%@%@", API_HTTP_PREFIX, API_LOGIN] parameters:@{@"username" : _username.text, @"password" : _password.text, @"deviceId" : @"123", @"role": @"0"} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) { NSString *response = operation.responseString; NSLog(@"response = %@", response); NSData* data=[response dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"获取到的数据为:%@",dict); int retCode = [[dict objectForKey:@"code"] intValue]; NSLog(@"retCode为:%d",retCode); if (retCode == 200) { NSDictionary *dict_data = [dict objectForKey:@"data"]; NSLog(@"获取到的数据data为:%@",dict_data); NSLog(@"nickname = %@", [dict_data objectForKey:@"nickname"]); User *user = [[User alloc]initWithDictionary:dict_data]; [user toString]; NSLog(@"user username = %@",user.userName); [Config saveUserInfo:user]; [Config saveOwnAccount:_username.text andPassword:_password.text]; //页面跳转 HomeTabBarController *htbc = [[HomeTabBarController alloc]init]; [self presentViewController:htbc animated:YES completion:nil]; } else { _hud.mode = MBProgressHUDModeCustomView; _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HUD-error"]]; _hud.labelText = [NSString stringWithFormat:@"错误:%@", [dict objectForKey:@"msg"]]; [_hud hide:YES afterDelay:1]; } } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { NSLog(@"Error: %@", error); _hud.mode = MBProgressHUDModeCustomView; _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HUD-error"]]; _hud.labelText = [@(operation.response.statusCode) stringValue]; _hud.detailsLabelText = error.userInfo[NSLocalizedDescriptionKey]; [_hud hide:YES afterDelay:1]; } ]; }
刚刚的网络访问代码中已经包含了页面跳转,我们这里再回顾下
//页面跳转 HomeTabBarController *htbc = [[HomeTabBarController alloc]init]; [self presentViewController:htbc animated:YES completion:nil];
网络访问一般返回的数据类型是JSON或者XML,JSON使用的频率更多,在第一节登录的代码中包含了解析
NSString *response = operation.responseString; NSLog(@"response = %@", response); NSData* data=[response dataUsingEncoding:NSUTF8StringEncoding]; //将JSON的字符串解析为字典类型 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"获取到的数据为:%@",dict); int retCode = [[dict objectForKey:@"code"] intValue]; NSLog(@"retCode为:%d",retCode); if (retCode == 200) { NSDictionary *dict_data = [dict objectForKey:@"data"]; NSLog(@"获取到的数据data为:%@",dict_data); NSLog(@"nickname = %@", [dict_data objectForKey:@"nickname"]); User *user = [[User alloc]initWithDictionary:dict_data]; //使用字典初始化User对象 [user toString]; NSLog(@"user username = %@",user.userName); }