http://onevcat.com/2013/08/ios7-background-multitask/
iOS provides the following multiple ways to handle background tasks
1:beginbackgroundtaskwithexpirationhandler
2: Background processing for a specific task
3: Background Access
4: Push Wakeup
5: Background transfer
The following 3 ways of IOS7 support
Beginbackgroundtaskwithexpirationhandler
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions{_count = 0 1 Target:self selector: @selector (function) Userinfo:nil Repeats:yes]; return Yes;} -(void ++; NSLog ( @ " function:%d " ,_count);}
The above code turns on a timer, outputting the value of count every second, but when our app is back to the foreground the timer stops running, We can request more time to run our code through the function Beginbackgroundtaskwithexpirationhandler to the system, and this "more time" has been tested for about 3 minutes.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{_count=0; [Nstimer scheduledtimerwithtimeinterval:1target:self selector: @selector (function) Userinfo:nil Repeats:yes]; returnYES;}-(void) function {_count++; NSLog (@"function:%d", _count);}- (void) Applicationdidenterbackground: (UIApplication *) application{_identifier= [Application beginbackgroundtaskwithexpirationhandler:^{NSLog (@"SS"); [Application Endbackgroundtask:_identifier]; _identifier=Uibackgroundtaskinvalid; }];}
_identifier is an integer variable, Beginbackgroundtaskwithexpirationhandler sets a time-out callback function that, after more than 3 minutes, the background task will be suspended while executing the timeout function. We should call Endbackgroundtask in the timeout function, and the following code shows the more general way to call Beginbackgroundtaskwithexpirationhandler
- (void) Beginbackgroundtask {uiapplication*application =[UIApplication sharedapplication]; _identifier= [Application beginbackgroundtaskwithexpirationhandler:^{[Self endbackgroundtask]; }];}- (void) Endbackgroundtask {uiapplication*application =[UIApplication sharedapplication]; [Application Endbackgroundtask:_identifier]; _identifier=Uibackgroundtaskinvalid;}- (void) Applicationdidenterbackground: (UIApplication *) application{[self beginbackgroundtask]; //Background Tasks[self endbackgroundtask];}
Background Access
Background acquisition is the IOS7 new content, its core role is to set an interval, and then every time to wake up the application to handle the corresponding tasks, such as the social software we use, we can get the latest information every time, so the next time we enter after we do not need to wait for the refresh, use the background to get the following steps:
1: Add app support for background gain, you can modify Uibackgroundmode in the Plist file, add fetch, or tick background in capabilities->background modes of application information Fetch
2: Set minimum background acquisition time interval
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ [Application setminimumbackgroundfetchinterval:uiapplicationbackgroundfetchintervalminimum]; return YES;}
If not set, the default is Uiapplicationbackgroundfetchintervalnever, which means no acquisition, and this value represents the "minimum" background acquisition interval, where the time interval specified here represents only " After the last acquisition or closure of the application, in this period of time will not do background gain ", iOS does not wake up the CPU for every application, the specific wake-up time depends on the system scheduling, Set to Uiapplicationbackgroundfetchintervalminimum to make it possible to wake up our app in the background, so the disadvantage of this setting is power consumption.
3: Implement Application:performfetchwithcompletionhandler
- (void) Application: (UIApplication *) application Performfetchwithcompletionhandler: (void(^) (Uibackgroundfetchresult)) Completionhandler {nsurlrequest*request = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.cnblogs.com/zanglitao/"]]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiondatatask*task = [Session datataskwithrequest:request completionhandler:^ (NSData *data, Nsurlresponse *response, NSError *error) { if(Error) {NSLog (@"%@", [Error localizeddescription]); Completionhandler (uibackgroundfetchresultfailed); } Else { ////Update UINSLog (@"%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]); Completionhandler (Uibackgroundfetchresultnewdata); } }]; [Task resume];}
Similar to Beginbackgroundtaskwithexpirationhandler, the system provides a callback Completionhandler to notify the system that the task is complete
Push Wakeup
1: Add Remote-notification in Uibackgroundmodes
2: Change push payload: Need to add content-available in payload and set to 1
3: Implementing push wake-up code
-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler
The implementation of the code is consistent with the previous one, which calls the callback method to notify the system
Background transfer
IOS7 provides nsurlsession alternative nsurlconnection for data transfer, nsurlsession usage before
iOS background tasks