-3

When iphone app is enter in background mode then ios automatically terminate app after 180 second. If we require to continuously data sync between iphone app to iwatch app. In this situation what can we do?

How to continuously data sync enable between iphone and iwatch when iphone app is in background and time was expired.

Lets see answer for more details.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Shahabuddin Vansiwala
  • 673
  • 1
  • 13
  • 24
  • There is no need of any Background handling for iOS because if you are using WCSession's background methods it will automatically wakes up your iPhone app. – Mrug Apr 27 '16 at 11:09
  • You are right. When you put your iPhone app in background then after 180 second you can't longer access to iphone app and never send a request from iwatch to iphone to show data in iwatch. (In the case of local database we are using in iPhone app) – Shahabuddin Vansiwala Apr 27 '16 at 13:58

1 Answers1

-1

In this situation we can write below code for continuously execute our app in background.

AppDelegate.h

Bool *isBackground = NO;
- (void)applicationDidEnterBackground:(UIApplication *)application {

   NSLog(@"Enter Background");

   [self extendBackgroundRunningTime];  //Call function for upadte time

   NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);

   NSLog(@"%d",_isFromBackground);
}

Function

- (void)extendBackgroundRunningTime {
    if (_isFromBackground != UIBackgroundTaskInvalid) {
        return;
    }
    NSLog(@"Attempting to extend background running time");

    __block Boolean self_terminate = YES;

    _isFromBackground = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
        NSLog(@"Background task expired by iOS");
        if (self_terminate) {
            [[UIApplication sharedApplication] endBackgroundTask:_isFromBackground];
            _isFromBackground = UIBackgroundTaskInvalid;
        }
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Background task started");

        while (true) {
            NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
            [NSThread sleepForTimeInterval:10];
        }

    });
}

Hope this may help you.

Shahabuddin Vansiwala
  • 673
  • 1
  • 13
  • 24
  • Where is the code of Watch-iOS Communication ? Either your Question is wrong or your answer, Better you decide first. This is the Background Expiration extending and your question is about background syncing. – Mrug Apr 27 '16 at 11:06
  • We want to extend the time in our app by 1 minute but the method described above seems to have stopped working since iOS 11 - if the phone is left untouched. Seems like the OS is stopping any sort of timers running in the background. Any suggestions how this might work in iOS 11? – Randeep Jun 30 '18 at 06:34
  • if set variable self_terminate = NO , I managed to keep the application running for more than 15 minutes! – Anonimys Jul 17 '19 at 11:18