Anu Padhye
Anu Padhye

Reputation: 615

Running Background Service in iOS

I am a new bee in iOS apps development. I want to create a background service in iOS. I also want to mention a particular time of the day and interval (every 2 hours) to call the service (kind of an Alarm to invoke service). When the service runs, some task should get performed independent of the state of app i.e. app can be in active or inactive state. Any example of it or a code snippet of implementation will help me implement this functionality.

Upvotes: 2

Views: 706

Answers (1)

Gopal Raju
Gopal Raju

Reputation: 246

Use this Code

- (void)applicationWillEnterForeground:(UIApplication *)application
{

[Timer invalidate];
    Timer = nil;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Background process is Start(EnterBackground)!");
    __block UIBackgroundTaskIdentifier bgTask ;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];
    Timer = [NSTimer scheduledTimerWithTimeInterval:120.0f target:self  selector:@selector(process)  userInfo:nil repeats:YES];
}

Upvotes: 3

Related Questions