" Push Wake (Remote notifications)"
In IOS6 and before, the type of push is very single, nothing more than to display the title content, specify the sound and so on. After the user unlocks into your app, the callback that opens the app via push in Appdelegate is called, and then you get the data to display. This is the same as when there is no background gain when you open the app and then get the data refresh.
This behavior has changed in iOS7, and we have the opportunity to have the device wake the device and our backend app after receiving the remote push, and execute a piece of code to prepare the data and UI before prompting the user for a push. At this point, if the user unlocks the device into the application will no longer have any loading process, the new content will be directly rendered.
1. Enable push wakeup.
change Info.plist, in UIBackgroundModes under Join remote-notification can be opened, of course, the same simpler and more straightforward way is to use capabilities.
2, change the push payload.
In iOS7, if you want to use push to wake up the app running code, you need to add content-available the payload , and set to 1.
1 APS { 2 13 alert: {...} 4 }
3. Implement push wake-up code and notify the system.
implemented ?-application:didReceiveRemoteNotification:fetchCompletionHandle: in Appdelegate . This part of the content background gets exactly the same.
Apple will limit the frequency of this analogy, and when the frequency exceeds a certain limit, push with the content-available flag will be blocked to ensure that the user device is not woken up frequently. According to Apple, this frequency will not be too much of a problem to push in a single-digit number of times within an hour.
" Background transfer (? Background Transfer Service)"
IOS7 introduced the background transmission of the relevant way, to ensure that the application after the data download or upload can continue. This transfer is managed by the iOS system, with no time limit, and does not require the app to run in the foreground.
To implement a background transfer, you must use IOS7 's new network connection class, Nsurlsession. This is the class introduced in iOS7 to replace the stale nsurlconnection.
1, create nsurlsession.
1-(Nsurlsession *) Backgroundsession2 {3 //Use dispatch_once_t to create only one background session. If you want more than one session, does with different identifier4 StaticNsurlsession *session =Nil;5 Staticdispatch_once_t Oncetoken;6Dispatch_once (&oncetoken, ^{7Nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration backgroundsessionconfiguration:@"com.yourcompany.appId.BackgroundSession"];8session = [Nsurlsession sessionwithconfiguration:configurationDelegate: Self delegatequeue:nil];9 });Ten returnsession; One}
2, add the corresponding transmission with the Nsurlsessiontask, and start the download.
1 //@property (nonatomic) nsurlsession *session;2 //@property (nonatomic) nsurlsessiondownloadtask *downloadtask;3 4-(Nsurlsession *) Backgroundsession5 {6 //...7 }8 9- (void) BegindownloadTen { OneNsurl *downloadurl =[Nsurl urlwithstring:downloadurlstring]; ANsurlrequest *request =[Nsurlrequest Requestwithurl:downloadurl]; -Self.session =[self backgroundsession]; -Self.downloadtask =[Self.session downloadtaskwithrequest:request]; the [Self.downloadtask resume]; -}
3, the last step is to achieve in the Appdelegate-application:handleEventsForBackgroundURLSession:completionHandler:
1 //APPDELEGATE.M2- (void) Application: (UIApplication *) application handleeventsforbackgroundurlsession: (NSString *) Identifier3Completionhandler: (void(^) ()) Completionhandler4 {5 //Check If all transfers is done, and update UI6 //Then -Tell system background transfer-over, so it can take the new snapshot to show in App switcher7 Completionhandler ();8 9 //You can also pops up a local notification to remind the userTen //... One}
Once the status of the background transfer changes (including normal end and failure), the app will be woken up and run the callback in Appdelegate , and then the Nsurlsessiontask delegate method will be called in the background . Although the above example directly in the Appdelegate call Completionhandler, but in fact, the better choice is to temporarily hold Completionhandler in Appdelegate, Then, in Nsurlsessiontask's Delegate method, check to see if the transfer is complete and the UI is updated before calling Completionhandler. In addition, your app is only running in the background so far, and if you want to remind the user that the transfer is complete, you may also need to send a local push at this time (remember that your app can execute code at this time, although it's in the background) so that the user can notice the changes in your app and go back to the app, And begin to have the data and interface ready.
Background transfers will only be done via WiFi.
Reference: http://onevcat.com/2013/08/ios7-background-multitask/