iOS 开发笔记

目录


  • 网络请求
    • POST方法
    • GET方法
  • 页面加载

网络请求

  • POST方法

需要借助AFNetworking

    NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx";
    NSDictionary *param = @{@"key":@"value"};
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败%@", error);
    }];
  • GET方法

需要借助AFNetworking

    NSString *url = @"http://xxxxxxxxxxxxxxxxxxxx; //网址
    NSDictionary *param = @{@"key":@"value"};//参数
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSLog(@"KEY :%@",[json valueForKey:@"KEY"]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误:%@",error);
    }];
iOS 开发笔记_第1张图片
image.png

你可能感兴趣的:(iOS 开发笔记)