简体   繁体   English

仅当 iPhone 与 xcode 连接时,后台任务才会运行

[英]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大家好,使用以下代码进行后台任务它工作正常,当 iPhone 与 xcode 连接时,但是当我在没有连接 xcode 的情况下运行应用程序时,后台任务将无法工作

- (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 http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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