1
  • (void)applicationWillResignActive:(UIApplication *)application {

    NSLog(@"to background");

    UIApplication *app = [UIApplication sharedApplication];

    // Request permission to run in the background. Provide an // expiration handler in case the task runs long. NSAssert(bgTask == UIBackgroundTaskInvalid, nil);

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ // Synchronize the cleanup call on the main thread in case // the task actually finishes at around the same time. dispatch_async(dispatch_get_main_queue(), ^{

        if (bgTask != UIBackgroundTaskInvalid)
        {
            [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 performSelector:@selector(hai) withObject:nil afterDelay:30];
    
    NSLog(@"App staus: applicationDidEnterBackground");
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if (bgTask != UIBackgroundTaskInvalid)
        {
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }
    });
    

    });

}

uttam raj
  • 67
  • 1
  • 10
  • Possible duplicate of [How to send an email to a receipent in background in iOS5?](http://stackoverflow.com/questions/8083261/how-to-send-an-email-to-a-receipent-in-background-in-ios5) – Preetam Jadakar Oct 13 '15 at 06:17
  • 2
    Your application will run for 10 min max in background. So NSTimer works well for this situation. If you want to run your application more than 10 min, then please have a look at http://stackoverflow.com/a/9738707/988169 – pkc456 Oct 13 '15 at 06:18
  • You haven't said which operating system or shown your code and errors... – Wain Oct 13 '15 at 06:41
  • yosemite.and I tried the code below but it not working in IOS 8 – uttam raj Oct 13 '15 at 11:00

0 Answers0