Continued solution after nstimer enters the background

Source: Internet
Author: User

1. In info. plist, add the required background modes key. The value is app plays audio.

2. Declare the code in the program startup method (-(bool) Application: didfinishlaunchingwitexceptions :)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    // Override point for customization after application launch.    <span style="color:#FF6666;">NSError *setCategoryErr = nil;    NSError *activationErr  = nil;    [[AVAudioSession sharedInstance]     setCategory: AVAudioSessionCategoryPlayback     error: &setCategoryErr];    [[AVAudioSession sharedInstance]     setActive: YES     error: &activationErr];</span>    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

3. Add the following code to the-(void) applicationdidenterbackground :( uiapplication *) application function in the appdelegate file, or add the notification method after the application registered in a specific class enters 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;            }        });    });

Thank you for your criticism!

Continued solution after nstimer enters 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.