1

I am working with Afnetworking with my project and i want to download big file around 100mb or 150mb in background but in apple documentation they said that background task will last upto 10 mins so how should i solve this issue ?

I search on internet and i idle timer to disable it or set timer according to the file?( but how can i set idle timer according to my file size and it also depends on internet connection)..

In some SO post matte post this answer but i am not sure that he disable the idle timmer for this afnetworking background answer

(void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
    [application endBackgroundTask:backgroundTaskIdentifier];
    [[YourRestClient sharedClient] cancelAllHTTPOperations];
}];

this is my afnetworking code

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

    }


}
Community
  • 1
  • 1
Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

2 Answers2

0

There is no way to "get iOS to extend that 10m limit".

I believe you have 2 options:

  • First is that you can try to play around the "resume" parameter and you download as much as you can for the time allowed, then when the timer is out, you trigger a new background download resuming from previous download. be aware that you do NOT know how much time will be allowed, can be 10 minutes or 1 minute. No guarantee over that.
  • Second, there is a new "background mode" in iOS7 that is called "fetch". But again, it will prioritize "small and reasonable" downloads over bigger ones. So the more you download, the higher the penalty.

Other than that, you'll need to do it in your UI with a loader and ask your users to bare the download.

nembleton
  • 2,392
  • 1
  • 18
  • 20
0

Check out background transfer service, which is available as of iOS7: http://code.tutsplus.com/tutorials/ios-7-sdk-background-transfer-service--mobile-20595

jeremywhuff
  • 2,911
  • 3
  • 29
  • 33