Download file using AFNetworking on iOS 6

后端 未结 3 965
[愿得一人]
[愿得一人] 2021-01-15 15:51

I\'ve recently updated to AFNetworking 2.0. The documentation said it is compatible with iOS6.0+. I am building a iOS 6.0 app, when I am trying to implement a download metho

3条回答
  •  旧时难觅i
    2021-01-15 16:15

    You can use the AFHTTPRequestOperation class to perform a file download on iOS 6. You basically just need to set the operation's outputStream property to store the file and the downloadProgressBlock property to monitor the progress.

    The bare bones method below is declared in a class that is a subclass of AFHTTPRequestOperationManager. When I initialized an instance of this class I set up the baseURL property.

    - (AFHTTPRequestOperation *)downloadFileWithContentId:(NSString *)contentId destination:(NSString*)destinationPath {
    
        NSString *relativeURLString = [NSString stringWithFormat:@"api/library/zipped/%@.zip", contentId];
        NSString *absoluteURLString = [[NSURL URLWithString:relativeURLString relativeToURL:self.baseURL] absoluteString];
    
        NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:absoluteURLString parameters:nil];
    
        void (^successBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^void(AFHTTPRequestOperation *operation, id responseObject) {
    
        };
    
        void (^failureBlock)(AFHTTPRequestOperation *operation,  NSError *error) = ^void(AFHTTPRequestOperation *operation,  NSError *error) {
    
        };
    
        AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:successBlock failure:failureBlock];
    
        NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:destinationPath append:NO];
        operation.outputStream = outputStream;
    
        [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    
        }];
    
        [self.operationQueue addOperation:operation];
    
        return operation;
    }
    

提交回复
热议问题