开发者

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 con开发者_C百科nected 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜