IOS 7 Background transmission stops after 3 minutes

I created a code sample to re-download the file from the network (every 30 seconds or so). In iOS 7 using background transfer services using NSURLSession
I followed this tutorial http://mobile.tutsplus.com/tutorials/iphone/ios-7-sdk-background-transfer-service/ and added this timer to repeat it.

    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
mute = [NSTimer scheduledTimerWithTimeInterval:30.0f
                                        target:self
                                      selector:@selector(startDownload)
                                      userInfo:nil
                                       repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:mute forMode:NSRunLoopCommonModes];

When I run it (in the background by pressing the home button) in the simulator and on the iPad connected to Xcode (where I can see the logs), everything works fine and it constantly loads. But when I disconnect the iPad from the Mac and launch it on the iPad in the background after about 3 seconds, it stops working (handleEventsForBackgroundURLSession in AppDelegate is called).

In the capabilities of the Xcode project, I chose Background fetch as the background modes.

What am I missing here or what did I do wrong to stop him after about 3 minutes? (According to the documents with the services of iOS 7 "Background Translation", it should work continuously, since there is no background for the background for this.)

thanks

+4
source share
5 answers

Background tasks in iOS7 will give you a maximum of 30 seconds (well below 10 minutes). Instead, you should use the background of the new selection. You should not use a timer, but use the newly provided API to ask the OS to wake up at regular intervals and configure boot using NSURLSession.

+4

- . ( , < 30s ). NSURLSessions , , , NSURLSessionDownloadTasks .

, , , , , . , NSURLSessionDownloadTasks (AC , WiFi ..). , Mac, . , . , .

, , , discretionary NSURLSessionConfiguration false. , , , true, FYI.

Apple : https://developer.apple.com/library/iOS/samplecode/SimpleBackgroundTransfer/Introduction/Intro.html

+3

. .

.h

UIBackgroundTaskIdentifier bgTask;

.m

//background task code
UIApplication *app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(startDownload) userInfo:nil repeats:YES];

yor

-(void)startDownload{

    NSLog(@"will log even if in background or foreground");
}
+2

[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler: ^{}]; 180 . Xcode .

+1

NSURLSession . . , . , Wi-Fi . , , . 30 , , .

I propose to abandon the use of Background migration services and configure everything using the background selection (which is completely different - by the way). Just be careful if you want to go to the store, you have to go into one of the common use cases for this feature to be approved for your application. If not, then there is little hope. Not sure what you are trying to do. Perhaps you really don't need to have as much background activity.

0
source