1

自从我使用GPS服务以来,我开发了一个在后台运行良好的应用程序。在

应用程序NSTimer在后台运行也IPhone 4很好IPhone 4s

IPhone5当应用程序处于后台时,它是如何暂停的。

我使用的代码NSTimer

repeatTimer5 =   [NSTimer scheduledTimerWithTimeInterval:1.0 target: self
                                                        selector: @selector(toupdate) userInfo: nil repeats: YES];


 [[NSRunLoop currentRunLoop]addTimer:repeatTimer5 forMode: NSDefaultRunLoopMode];

有没有人遇到过同样的问题?

4

1 回答 1

0

Use this : Fire your timer when app did entered into background in AppDelegate delegate method:

 - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        if(!repeatTimer5.isValid)
        {
repeatTimer5 =   [NSTimer scheduledTimerWithTimeInterval:1.0 target: self
                                                                 selector: @selector(toupdate) userInfo: nil repeats: YES];
        }


    }

    - (void)toupdate
    {
        DLog(@"Testing timer Fired");
    }

this will fire your timer method when app moved in foreground and will resumed when app moved in background.

If you still need to this timer will be fired in background mode then start BackgroundTask

- (void)applicationDidEnterBackground:(UIApplication *)application
    {
        if(!repeatTimer5.isValid)
         {
       [application beginBackgroundTaskWithExpirationHandler:^{
        //your background expiration handler method
    }];

    repeatTimer5 =   [NSTimer scheduledTimerWithTimeInterval:1.0 target: self
                                                             selector: @selector(toupdate) userInfo: nil repeats: YES];
          }


    }

    - (void)toupdate
    {
        DLog(@"Testing timer Fired");
    }

This will fire your timer when ur app is in background

于 2013-09-03T09:43:23.237 回答