NSURLSession의 body Parameter 체험

1461 단어
비 AFNetworking
- (void)requestPostPath:(NSString *)aPath bodyparams:(id)params block:(void (^)(id  data, NSError *error))block {
         NSMutableArray * parameters = [NSMutableArray new];
        //self.requestParameter 
        //1. 
        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            //2. key=value 
            [parameters addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
        }];
        //3. request
        NSMutableURLRequest * requst = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: aPath]];
        //4. , GET
        requst.HTTPMethod = @"POST";
        [requst setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"contentType"];
        //5. parameters & HTTPBody , : parameters ,
        requst.HTTPBody = [[parameters componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding];
        [[[NSURLSession sharedSession] dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSDictionary *datdica = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                block(datdica, error);
            });
        }] resume];
 }

괴로움의 고통을 이제 얻었으니 어찌 그리 빠르랴.

좋은 웹페이지 즐겨찾기