0

Hello everyone I need to call a method when the app is in background. This is now i am trying to achieve this but the timer is never called.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.
        self.timer = nil;
        [self initTimer];

    });
}

- (void)initTimer {

    DebugLog(@"timer INIT!!!!!");

    if (self.timer == nil) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
                                                      target:self
                                                    selector:@selector(checkUpdates:)
                                                    userInfo:nil
                                                     repeats:YES];
    }
}

- (void)checkUpdates:(NSTimer *)timer{
    [UIApplication sharedApplication].applicationIconBadgeNumber++;

    DebugLog(@"timer FIRED!!!!!");
    [self initTimer];
}

I need to call the method checkUpdates every Nth minute or seconds while the app is in background.Please note that initTimer is called when the app enters background but the timer is never called.

1 Answers1

-1

Apple will not allow the use the application in background if you want to use the application in background you have to use the 'UIBackgroundModes'.

and after that from Appdeligate you can manage this thread

you should check

http://developer.apple.com/library/ios/#documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

hope this will help you

Divyam shukla
  • 2,038
  • 14
  • 18
  • Hello there thanks for your reply but the goal behind it is to call StartUpdatingLocation every 5 minutes so that it wont drain the battery. I used Startmonitoringsignificantchanges but i need maximum acurracy. – droidiphonereef May 23 '13 at 09:26
  • you should use location manager and set the accuracy best after that you can start your timer and each n every 5 mins you can get call startUpdatingLocation and you should set the UIBackground mode for navigation app and then your work will be easily done – Divyam shukla May 23 '13 at 09:29
  • One more thing you should remove the code UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; from background method function – Divyam shukla May 23 '13 at 09:30
  • my timer is called only when my app goes in foreground.What i did is i initialise my locationmanger when app enters background and i init the Nstimer there i do startUpdatingLocation and when i enter the app i.e app is in foreground then it calls didupdatelocation. Any idea? – droidiphonereef May 23 '13 at 10:38
  • UIBackground mode for navigation app u mean in the plist files add the entries i already did – droidiphonereef May 23 '13 at 10:39
  • Have you remove the BGtask code from the mothod applicationDidEnterBackground ? – Divyam shukla May 23 '13 at 11:13
  • http://stackoverflow.com/questions/16297551/ioscapturing-user-location-clarification Check this link i Have already performed the same thing in Background – Divyam shukla May 23 '13 at 11:19
  • i am also working on like this functionality . so anyone could find the idea yet ? or anyone came up with some way ? how to and what codes are used in ios 10.12 ? please guide me through it . i am new in working with background services . – Moxarth Jun 23 '17 at 13:41
  • @Moxarth what kind of app it is it is using location services ? or is it using some music ? – Divyam shukla Jun 23 '17 at 14:06
  • I have made an app that gets the user location and stores in local servers using web service and whenever user location data are required can be fetched by calling web service. Now I want to add the functionality in such a way that when app is closed, in background state, or even in running state the method for fetching user location should call and keep on calling it in 5 to 10 minutes time interval and can call web services called in that code block. – Moxarth Jun 24 '17 at 04:51
  • I have referred to many tutorials Like [https://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520] and methods like this and [http://blog.dkaminsky.info/2013/01/27/keep-your-ios-app-running-in-background-forever/] -- [http://hayageek.com/ios-long-running-background-task/] to implement it, but could not come up with the method or way, or those codes are way too old. So, please guide me through it. I am new in performing background services. Let me know ,where I have to look and what methods should I have to add in it. I am using xib Objective-C - Xcode 8.3. – Moxarth Jun 24 '17 at 04:52
  • You need to add UIBackgroundModes into info.plist. when you enters in background start your timer and call your method. no need to do anything. – Divyam shukla Jun 27 '17 at 07:00