IOS开发随笔2

1、网络访问代码

     用户登录时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];
      }
     ];

}

2、页面跳转

     刚刚的网络访问代码中已经包含了页面跳转,我们这里再回顾下

     

//页面跳转
              HomeTabBarController *htbc = [[HomeTabBarController alloc]init];
              [self presentViewController:htbc animated:YES completion:nil];

3、JSON解析

     网络访问一般返回的数据类型是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);
              

          }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值