2

我最近更新到 AFNetworking 2.0。文档说它与iOS6.0+兼容。当我尝试实现下载方法(图像和视频)时,我正在构建一个 iOS 6.0 应用程序。示例使用

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

但是,我收到“使用未声明的标识符 'AFURLSessionManager'”错误。我发现 AFURLSessionManager 使用了一个只能从 iOS7 获得的类。我只是想知道,我可以使用 AFNetworking 在 iOS6 中下载谁?

另外,有没有看下载进度?

谢谢

4

3 回答 3

4

您可以使用AFHTTPRequestOperation该类在 iOS 6 上执行文件下载。您基本上只需要设置操作的outputStream属性来存储文件和downloadProgressBlock属性来监视进度。

下面的基本方法是在一个类中声明的,该类是AFHTTPRequestOperationManager. 当我初始化这个类的一个实例时,我设置了baseURL属性。

- (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;
}
于 2013-10-17T18:21:46.037 回答
3

试试这个...

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFHTTPRequestOperation *operation = [manager GET:urlString
                                      parameters:nil
                                         success:^(AFHTTPRequestOperation *operation, NSData *responseData)
                                     {
                                         [responseData writeToURL:someLocalURL atomically:YES];
                                     }
                                         failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                     {
                                         NSLog(@"Downloading error: %@", error);
                                     }];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
 {
     float downloadPercentage = (float)totalBytesRead/(float)(totalBytesExpectedToRead);

     [someProgressView setProgress:downloadPercentage animated:YES];
 }];
于 2014-07-08T12:42:13.083 回答
2

正如您所说AFURLSessionManager,仅在 iOS 7 中可用(由 支持NSURLSession),因此您应该使用NSURLConnectionAFNetworking 2.0 中的基础类(AFHTTPRequestOperationManager、、AFHTTPRequestOperation等)。

于 2013-10-10T12:31:15.233 回答