Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

timer - How to run NSTimer in background beyond 180sec in iOS 7?

I have tried this but not working more than 180 sec in iOS 7 and Xcode 4.6.2. Please help me

    UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
        UIApplication *app = [UIApplication sharedApplication];
       bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];
        NSTimer  *timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self   selector:@selector(timerMethod) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
     -(void) timerMethod{

     NSLog(@"in timerMethod");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unless you enable one of the Background modes, it is not gonna work.

Why?

  • You have around 10 minutes of background execution after this the timer is stopped by ios.

  • The timer will not fire after app is locked (iOS7), since ios suspends the foreground app and bgTask will not get fire again.

There is some workarounds, consider to check below question:

iphone - NSTimers in background

Scheduled NSTimer when app is in background?

NSTimer on background IS working


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...