The iOS program still runs the timer after it enters the background nstimer

Source: Internet
Author: User

Since this application needs to be timed when the app is in the background, it's a big requirement for me to keep the app running after it gets into the background. And then it's hard to do it in iOS, whether it's through audio or the location system, I've found some data that can only be applied to a 10-minute running state. There is no concept of long-term operation .....

However, in the blog suddenly see this article, try to be able to run for a long time. Anyway, I'll try it first ... As to whether or not Apple can audit by estimating is another difficult

Specific as follows:

This article is one of them: iOS program into the background within 10 minutes will be killed by the system, how to solve it? I want the program into the background and still run the timing function, otherwise it will not be able to achieve the purpose of the exam, after the online access to the relevant information finally found the answer, the essence is: the use of Apple three types of programs can be kept in the background run: Audio playback class, location update class, the other one is not very clear, I took advantage of the "privilege" of the audio playback class given by Apple to meet my program requirements, with the following steps:

1, Step one: in Info.plist, add "Required background Modes" key, value is: App plays audio

Step Two:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];    Override point for customization after application launch.    Nserror *setcategoryerr = nil;    Nserror *activationerr  = nil;    [[Avaudiosession sharedinstance]     setcategory:avaudiosessioncategoryplayback     error: &setCategoryErr];    [[Avaudiosession sharedinstance]     setactive:yes     error: &activationErr];    Self.window.backgroundColor = [Uicolor whitecolor];    [Self.window makekeyandvisible];    return YES;}

Step Three: Add the following code to the Appdelegate file-(void) Applicationdidenterbackground: (uiapplication *) application function, You can also add to the notification method after the app is registered in a specific class to enter the background

-(void) Applicationdidenterbackground: (uiapplication *) application{    uiapplication*   app = [uiapplication Sharedapplication];    __block    uibackgroundtaskidentifier bgtask;    Bgtask = [App beginbackgroundtaskwithexpirationhandler:^{        Dispatch_async (Dispatch_get_main_queue (), ^{            if ( Bgtask! = uibackgroundtaskinvalid)            {                bgtask = Uibackgroundtaskinvalid;            }}        );    }];    Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{        Dispatch_async (dispatch_ Get_main_queue (), ^{            if (bgtask! = uibackgroundtaskinvalid)            {                bgtask = uibackgroundtaskinvalid;            }        });    });}

By completing the above steps you will find that the timer can still be run after the program enters the background!

Thanks http://www.cnblogs.com/zzltjnh/archive/2013/05/15/3080058.html! I hope I'm useful here.

Look closely at Apple's official documents later ...

The iOS program still runs the timer after it enters the background nstimer

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.