我后来是这么解决不知道行不行,能够长期的在后台执行
首先我在xx-info.plist 里的 "Required background modes" 里增加"App provides Voice over IP services"
然后在delegate里增加下面代码,原理是进入后台时程序会在600秒那样结束任务,我做的就是在结束任务前新开一个任务。再结束旧任务,这样就一直的在后台执行,希望可能帮助到很多其它的人。我也查了非常久才找到这种方法的。
UIBackgroundTaskIdentifier
backgroundTaskIdentifier;
|
02
|
UIBackgroundTaskIdentifier
oldBackgroundTaskIdentifier;
|
04
|
-
(BOOL) isMultitaskingSupported{
|
08
|
if ([[UIDevice
currentDevice]
|
10
|
respondsToSelector:@selector(isMultitaskingSupported)]){
result = [[UIDevice currentDevice] isMultitaskingSupported];
|
18
|
-
( void )
timerMethod:(NSTimer *)paramSender{
|
20
|
if (count
% 500 == 0) {
|
21
|
UIApplication
*application = [UIApplication sharedApplication];
|
25
|
backgroundTaskIdentifier
= [application beginBackgroundTaskWithExpirationHandler:^{
|
29
|
[application
endBackgroundTask:backgroundTaskIdentifier];
|
30
|
oldBackgroundTaskIdentifier
= backgroundTaskIdentifier;
|
34
|
-
( void )applicationDidEnterBackground:(UIApplication
*)application
|
36
|
if ([self
isMultitaskingSupported] == NO){
|
41
|
backgroundTaskIdentifier
= [application beginBackgroundTaskWithExpirationHandler:^{
|
43
|
oldBackgroundTaskIdentifier
= backgroundTaskIdentifier;
|
44
|
if ([self.myTimer
isValid]) {
|
45
|
[self.myTimer
invalidate];
|
47
|
self.myTimer
= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];
|
50
|
-
( void )applicationWillEnterForeground:(UIApplication
*)application
|
52
|
if (backgroundTaskIdentifier
!= UIBackgroundTaskInvalid){
|
53
|
[application
endBackgroundTask:backgroundTaskIdentifier];
|
54
|
if ([self.myTimer
isValid]) {
|
55
|
[self.myTimer
invalidate];
|
60
|
-
( void )applicationWillEnterForeground:(UIApplication
*)application
|
62
|
if (backgroundTaskIdentifier
!= UIBackgroundTaskInvalid){
|
63
|
[application
endBackgroundTask:backgroundTaskIdentifier];
|
64
|
if ([self.myTimer
isValid]) {
|
65
|
[self.myTimer
invalidate];
|