开发者

Background task is running only when iPhone is connected with xcode

开发者 https://www.devze.com 2023-03-21 19:04 出处:网络
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

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

精彩评论

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