to run app continuously in the background

前端 未结 3 526
不知归路
不知归路 2020-12-06 07:28

I want to my app to keep running in the background with location services. For this I have used:

-(void)applicationDidEnterBackground:(UIApplication *)applic         


        
3条回答
  •  暖寄归人
    2020-12-06 08:16

    I know this is very late for an answer. But if you still haven't got the answer, here is what I did and my app continuously runs in the background.

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
         UIApplication *app = [UIApplication sharedApplication];
         UIBackgroundTaskIdentifier bgTask = 0;
    
         backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self  selector:@selector(backgroundTask) userInfo:nil repeats:YES];
    
         bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];
    }
    

    Now do whatever you want in the backgroundTask method.

提交回复
热议问题