AFNetworking后台下载大文件

时间:2013-08-12 10:40:05

标签: ios download afnetworking background-process

我正在使用Afnetworking和我的项目,我想在后台下载大约100mb或150mb的大文件,但是在苹果文档中他们说后台任务将持续10分钟,所以我该如何解决这个问题?

我在互联网上搜索我idle timer禁用它或根据文件设置计时器?(但我如何根据我的文件大小设置空闲计时器,它还取决于互联网连接)..

在某些SO帖子中,我发布了这个答案,但我不确定他是否会为此afnetworking background answer

禁用闲置音色
(void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
    [application endBackgroundTask:backgroundTaskIdentifier];
    [[YourRestClient sharedClient] cancelAllHTTPOperations];
}];

这是我的afnetworking代码

        NSURL *url = [NSURL URLWithString:downloadMainURL];

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
        [httpClient.operationQueue setMaxConcurrentOperationCount:1];


        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:downloadPostUrl parameters:postRequest];

        AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:destPath shouldResume:YES];


        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if(operation.response.statusCode == 200) {

                 NSLog(@"enable ipad sleep");
                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                 [loadingHUD hide:TRUE];
              //   NSLog(@"responseString is %@",[operation responseString]);
                              }
            else{
                NSLog(@"setCompletionBlockWithSuccess");
                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                [loadingHUD hide:TRUE];
            }

        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            if(operation.response.statusCode!= 200) {

                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                [loadingHUD hide:TRUE];
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"download failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }
        }];

        [httpClient enqueueHTTPRequestOperation:operation];

        [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
         {

             int filesize = [[NSUserDefaults standardUserDefaults] integerForKey:@"filesize"];
             int totalbytes=filesize+bytesWritten;


             NSLog(@"productidFileSize value is %f",productidFileSize);
             NSLog(@"totalBytesWritten value is %d",totalbytes);

             progress = ((float)totalbytes )/ productidFileSize;
             NSLog(@"progress value is %f",progress);
             loadingHUD.progress = progress;
             [[NSUserDefaults standardUserDefaults] setInteger:totalbytes forKey:@"filesize"];
                }];

    }


}

2 个答案:

答案 0 :(得分:0)

没有办法“让iOS延长10米的限制”。

我相信你有两个选择:

  • 首先,您可以尝试播放“resume”参数,并在允许的时间内尽可能多地下载,然后当计时器停止时,您将触发从之前下载恢复的新后台下载。 请注意,您不知道将允许多长时间,可以是10分钟或1分钟。不能保证。
  • 其次,iOS7中有一个名为“fetch”的新“后台模式”。但同样,它将优先考虑“小而合理”的下载而不是更大的下载。所以你下载的越多,惩罚越高。

除此之外,您需要使用加载程序在UI中执行此操作,并要求您的用户免费下载。

答案 1 :(得分:0)