简体   繁体   中英

Background task is running only when iPhone is connected with xcode

Hi all using following code for background task it works fine,when iPhone is connected with xcode, but when I ran the app without connected xcode,then background tasks won't work

- (void)applicationDidEnterBackground:(UIApplication *)application
 {
    back=1.0f;

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSRunLoop *runLoop=[NSRunLoop currentRunLoop];
    timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeCounter) userInfo:nil repeats:YES];
    [runLoop run];
    [pool release];
 }

Please help why this is happening

have you checked the documentation for background executation?

you should start the task like:

- (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.

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM