How to run NSTimer in background and sleep in iOS?

后端 未结 6 639
南笙
南笙 2021-02-04 21:56

I found a lot of post in stackoverflow about NSTimer run in background.

However I didn\'t find any solution.

In my app , I play sound in background

6条回答
  •  广开言路
    2021-02-04 22:46

    Get Form [This Question] (http://iphonedevsdk.com/forum/iphone-sdk-development/58643-keep-nstimer-running-when-app-is-in-background-multitasking.html)

    - (void)btnSetupNotificationClicked:(id)sender
    {
        UILocalNotification* pOrderCompletedNotification=[[UILocalNotification alloc] init];
        if(pOrderCompletedNotification!=nil)
        {
            [pOrderCompletedNotification setFireDate:[[NSDate alloc] initWithTimeIntervalSinceNow:5.00]];
    //      [pOrderCompletedNotification setApplicationIconBadgeNumber:1];
            [pOrderCompletedNotification setTimeZone:[NSTimeZone systemTimeZone]];
            [pOrderCompletedNotification setSoundName:@\"OrderCompleted.m4a\"];
            [pOrderCompletedNotification setAlertBody:@\"Order Completed\"];
            [pOrderCompletedNotification setAlertAction:nil];
            [pOrderCompletedNotification setHasAction:NO];
    
            UIApplication* pApplication=[UIApplication sharedApplication];
            if(pApplication!=nil)
            {
                [pApplication scheduleLocalNotification:pOrderCompletedNotification];
            }
            else
            {
                NSLog(@\"Application singleton allocation error.\");
            }
    
            [pOrderCompletedNotification release];
            [pApplication release];
        }
        else
        {
            NSLog(@\"Local notification creation error.\");
        }   // if
    }
    

提交回复
热议问题