10

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");
    }
Deepak Gupta
  • 979
  • 1
  • 10
  • 18
  • checkout this: http://stackoverflow.com/a/9623490/905514 – A J Mar 25 '14 at 08:51
  • @Arvind-Systematix actually i have xcode 4.6.2 version . Will this soln work on that Xcode version? – Deepak Gupta Mar 25 '14 at 09:41
  • @Arvind-Systematix i need this functionality for IOS 7 and xcode 4.6.2 version. – Deepak Gupta Mar 25 '14 at 09:48
  • How you are using IOS 7 with Xcode 4.6 as it was introduced in XCode 5? http://en.wikipedia.org/wiki/Xcode. While with IOS 7, you have feature to on/off refreshing app in background. – A J Mar 25 '14 at 09:51
  • I agree with your point, but my project is developed in Xcode 4.6.2 and it is working fine on iOS 6 device but not on iOS7 device – Deepak Gupta Mar 25 '14 at 10:14

1 Answers1

11

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

Community
  • 1
  • 1
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68