AFNetWorking 3.0 usage

1. File download

AFHTTPSessionManager *manager=[AFHTTPSessionManagermanager];

    NSURLRequest *requestUrl=[NSURLRequestrequestWithURL:[NSURLURLWithString:url]];

    

    NSURLSessionDownloadTask *task=[manager downloadTaskWithRequest:requestUrl progress:^(NSProgress *downloadProgress){

        

        

        

        DLog ( @"- download progress ---%f" ,downloadProgress.fractionCompleted);

        

        dispatch_sync(dispatch_get_main_queue(), ^(){

            

            [uiprogressviewsetProgress:downloadProgress.fractionCompletedanimated:YES];

        });

        

        

    

    } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){

    

        return [NSURLfileURLWithPath:downloadpath_now];

        

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error){

    

        DLog ( @"---- download complete " );

    

    }];

    

 

    [task resume];

 

 

2. post request

 

AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];

    manager.requestSerializer.timeoutInterval = 30;

 

    manager.responseSerializer = [AFHTTPResponseSerializerserializer];

 

[manager POST:requestUrl parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

            

            NSString *filePath=[parameters objectForKey:@"IMAGE"];

            UIImage *image=[UIImageimageWithContentsOfFile:filePath];

            

            NSData *imageData =UIImageJPEGRepresentation(image,1);

            

            NSDateFormatter *formatter = [[NSDateFormatteralloc] init];

            formatter.dateFormat =@"yyyyMMddHHmmss";

            NSString *str = [formatter stringFromDate:[NSDatedate]];

            NSString *fileName = [NSStringstringWithFormat:@"%@.jpg", str];

            

            // Upload parameters ( upload image, in file stream format )

            [formData appendPartWithFileData:imageData

                                        name:@"IMAGE"

                                        fileName:fileName

                                        mimeType:@"image/jpeg"];

            

        } progress:^(NSProgress *_Nonnull uploadProgress) {

            // print upload progress

            

            DLog ( @" Upload progress ----%lld-" ,uploadProgress.completedUnitCount);

            

        } success:^(NSURLSessionDataTask *_Nonnull task, id_Nullable responseObject) {

            // upload successfully

            

            DLog(@"请求成功===response:%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

           

                

           }else{

                

               

                

            }

            

 

            

        } failure:^(NSURLSessionDataTask *_Nullable task, NSError * _Nonnull error) {

            // upload failed

           

 

        }];

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326477420&siteId=291194637