【问题标题】:Download file using AFNetworking on iOS 6在 iOS 6 上使用 AFNetworking 下载文件
【发布时间】:2013-10-10 12:25:08
【问题描述】:

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

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

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

还有,有没有可以查看下载进度的?

谢谢

【问题讨论】:

    标签: ios6 afnetworking-2


    【解决方案1】:

    您可以在iOS 6 上使用AFHTTPRequestOperation 类执行文件下载。您基本上只需要设置操作的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;
    }
    

    【讨论】:

      【解决方案2】:

      试试这个...

      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];
       }];
      

      【讨论】:

        【解决方案3】:

        正如您所说,AFURLSessionManager 仅在 iOS 7 中可用(由NSURLSession 支持),因此您应该在 AFNetworking 2.0 中使用基于NSURLConnection 的类(AFHTTPRequestOperationManagerAFHTTPRequestOperation 等)。

        【讨论】:

        • 好的,找到了这个......所以回顾一下:为了支持新的 NSURLSession API 以及旧的但不弃用且仍然有用的 NSURLConnection,核心组件AFNetworking 2.0 分为请求操作和会话任务。 AFHTTPRequestOperationManager 和 AFHTTPSessionManager 提供类似的功能,具有几乎可互换的接口,如果需要(例如在 iOS 6 和 7 之间移植),可以很容易地换出。
        • 您有使用 AFHTTPRequestOperationManager 下载文件的示例或教程吗?
        • 我没有,但它应该很简单,你只需要创建一个 AFHTTPRequestOperation(你可以使用管理器的方法 HTTPRequestOperationWithRequest:success:failure:) 并将其 outputStream 设置为指向文件的一个。
        猜你喜欢
        • 1970-01-01
        • 2012-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多