iOS running in the background

Source: Internet
Author: User

The sample code that lets the program run long in the background is as follows:

Running in the background

Another use of block is to allow the program to run longer in the background. In the past, when the app was left on the home button, the app only had a maximum of 5 seconds to do some saving or cleanup work. But the app can call the UIApplication beginBackgroundTaskWithExpirationHandler method, allowing the app to run for up to 10 minutes in the background. This time can be used to clean up the local cache, send statistics and other work.

//AppDelegate.h File@property (Assign, nonatomic) Uibackgroundtaskidentifier backgroundupdatetask;//appdelegate.m File- (void) Applicationdidenterbackground: (UIApplication *) application{[self beingbackgroundupdatetask]; //Add the code that you need to run long[self endbackgroundupdatetask];}- (void) beingbackgroundupdatetask{Self.backgroundupdatetask= [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{[Self endbackgroundupdatetask]; }];}- (void) endbackgroundupdatetask{[[UIApplication sharedapplication] endBackgroundTask:self.backgroundUpdateTask]; Self.backgroundupdatetask=Uibackgroundtaskinvalid;}

Arranging the delivery of local notification

The Uilocalnotification class provides a way to pass the local notifications. Unlike push notifications, where remote server needs to be set up, local notifications is scheduled and executed on the current device in the program. The ability can be used to meet the following conditions:

1, a time-based program, you can at a certain time in the future to the program post an alert, such as alarm clock

2, a program running in the background, post a local notification to attract the user's attention

In order to arrange the delivery of the local notification, you need to create an instance of Uilocalnotification and set it up, using the UIApplication class method to arrange it. The Local notification object contains the type to be passed (Sound,alert, or badge) and when the time is rendered. The UIApplication class method provides the option to determine whether to pass immediately or at a specified time.

- (void) Schedulealarmfordate: (nsdate*) thedate {uiapplication* App =[UIApplication sharedapplication]; Nsarray* Oldnotifications =[app Scheduledlocalnotifications]; //Clear out the old notification before scheduling a new one.     if([Oldnotifications Count] >0) [app Cancelalllocalnotifications]; //Create a new notification. uilocalnotification* alarm =[[Uilocalnotification alloc] init]; if(alarm) {alarm.firedate=thedate; Alarm.timezone=[Nstimezone Defaulttimezone]; Alarm.repeatinterval=0; Alarm.soundname=@"ALARMSOUND.CAF"; Alarm.alertbody=@"Time to Wake up!";      [App Schedulelocalnotification:alarm]; }  } 

(Can contain up to 128 local notifications active in any given time, any of the which can be configured to repeat at a specified interval .) If the program is already in the foreground when calling the notification, then Application:didreceivelocalnotification: The method will be replaced.

iOS running in the background

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.