[Turn]ios7 background execution

Source: Internet
Author: User

[Transfer from: http://esoftmobile.com/2013/06/23/ios7%E7%A8%8B%E5%BA%8F%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C/]

IOS7 Program Background Run

2013-06-23

Introduced

This time, the iOS7 to the background of the program to strengthen, but only to strengthen, to imagine the Android program as free of course, do not think, Apple is mainly due to battery use time to consider, but this time the strengthening of the majority of procedures are basically enough.

Before we introduce, let's review the knowledge behind the background run before iOS7. Before IOS7 (after iOS4) there are three main types of applications that can run in the background:

    1. Audio playback
    2. Background Location Services
    3. IP Phone

-(void) Applicationdidenterbackground: (uiapplication *) application {static Uibackgroundtaskidentifier task, task = [ Application beginbackgroundtaskwithexpirationhandler:^{task = Uibackgroundtaskinvalid;}; Perform background operations [application Endbackgroundtask:task]; } 

This time the IOS7 supports two new program background operating modes:

    • Programs that require regular data requests can be registered in the system so that the program can be woken up periodically in the background to download new data. This situation requires the Uibackgroundmodes entry in the program's Info.plist file to increase the fetch type, while the Setminimumbackgroundfetchinterval: method is used to set the minimum time interval for the program to get the data periodically. You need to implement the Application:performfetchwithcompletionhandler: Proxy method and perform the download operation within the method.

    • The program can also use the background message push service to notify the user that a new domestic demand can be downloaded, while activating the background download operation. This requires adding the Remote-notification value to the Uibackgroundmodes item, and you need to implement the Appdelegate method Application:didreceiveremotenotification: Fetchcompletionhandler: To perform your download operation.

Whether it is a program that supports fetch or remote-notification background run mode, it is possible that the system will be activated at the appropriate time or removed from the background pending state. In fetch mode, the system uses valid information to determine the best time to start or activate the program. For example, the system may allow the program to perform fetch operations when the network is in good condition or when the device has just been unlocked. A program that supports remote-notifiaction can be woken up when a push message is received, but before the user receives a push message, the program can download the latest content in the form of a regular fetch and be ready to present the content to the user before the subsequent push message.

In order to perform a background download, the program should use the new Nsurlsession class, which provides a cleaner, task-based interface to start and execute the Nsurlrequest object on the basis of the previous nsurlconnectoin. A Nsurlsession object can initiate multiple download or upload tasks and handle authentication requests from the server in the proxy method.

Realize

Now let's implement fetch and remote-notifiaction two kinds of background runs.

1. Settings

In the Xcode5.0 inside the capabilities can be directly checked to select the way the application needs to support the type of background run (can choose OH), we selected background fetch and remote notification two items. and add fetch and remote-notification two items in required background modes in the program's Info.plist file.

2. Background Fetch
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {// Override point for customization after application launch. [Application Setminimumbackgroundfetchinterval:uiapplicationbackgroundfetchintervalminimum]; return YES; }-(void) Application: (UIApplication *) application Performfetchwithcompletionhandler: (void (^) ( Uibackgroundfetchresult)) completionhandler{nsurl *url = [Nsurl urlwithstring:@ "http://127.0.0.1:3000/update.do"]; Nsurlsession *updatesession = [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]]; [Updatesession datataskwithhttpgetrequest:url completionhandler:^ (NSData *data, Nsurlresponse *response, NSError * Error) {nsdictionary *messageinfo = [nsjsonserialization jsonobjectwithdata:data options:0 Error:nil]; NSLog (@ "messageinfo:%@", messageinfo); Completionhandler (Uibackgroundfetchresultnewdata); }];  }

First set minimun background fetch in application:didfinishlaunchingwithoptions: The interval type is uiapplicationbackgroundfetchintervalminimum (default is Uiapplicationbackgroundfetchintervalnever), Then implement the Proxy method Application:performfetchwithcompletionhandler: To implement the data request.

In order to test the background of the program, we can create a new scheme, select Background fetch (Launch due to a background fetch event), and then run the program under the scheme, the program does not start, But you can see that it sends a request to the backstage.

Remote Notifications

Similar to implementing the Remote-notification mode, the Application:didReceiveRemoteNotification:fetchCompletionHandler needs to be implemented with the original push-supported conditions: Proxy method, which executes the proxy method when the program receives a push message in the background that contains "content-available = 1" in the payload. (because the simulator cannot simulate the message push, the ipad version of the iOS7 has not yet been offered for download, so I am temporarily unable to test).

Summarize

In general there is nothing too complicated to achieve, the key is how you apply these two new background running mode to your program.

[Go]ios7 background execution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.