我后来是这么解决不知道行不行,可以长期的在后台运行 首先我在xx-info.plist 里的 "Required background modes" 里加入"App provides Voice over IP services" 然后在delegate里加入以下代码,原理是进入后台时程序会在600秒那样结束任务,我做的就是在结束任务前新开一个任务,再结束旧任务,这样就一直的在后台运行,希望可能帮助到更多的人,我也查了很久才找到这个方法的。 UIBackgroundTaskIdentifier backgroundTaskIdentifier; UIBackgroundTaskIdentifier oldBackgroundTaskIdentifier; - (BOOL) isMultitaskingSupported{ BOOL result = NO; if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]){ result = [[UIDevice currentDevice] isMultitaskingSupported]; } return result; } - (void) timerMethod:(NSTimer *)paramSender{ count++; if (count % 500 == 0) { UIApplication *application = [UIApplication sharedApplication]; //开启一个新的后台 backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ }]; //结束旧的后台任务 [application endBackgroundTask:backgroundTaskIdentifier]; oldBackgroundTaskIdentifier = backgroundTaskIdentifier; } NSLog(@"%ld",count); } - (void)applicationDidEnterBackground:(UIApplication *)application { if ([self isMultitaskingSupported] == NO){ return; } //开启一个后台任务 backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ }]; oldBackgroundTaskIdentifier = backgroundTaskIdentifier; if ([self.myTimer isValid]) { [self.myTimer invalidate]; } self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES]; } - (void)applicationWillEnterForeground:(UIApplication *)application { if (backgroundTaskIdentifier != UIBackgroundTaskInvalid){ [application endBackgroundTask:backgroundTaskIdentifier]; if ([self.myTimer isValid]) { [self.myTimer invalidate]; } } } - (void)applicationWillEnterForeground:(UIApplication *)application { if (backgroundTaskIdentifier != UIBackgroundTaskInvalid){ [application endBackgroundTask:backgroundTaskIdentifier]; if ([self.myTimer isValid]) { [self.myTimer invalidate]; } } }