无法endBackgroundTask:没有标识符为1fd57580的后台任务,或者它可能已经结束

问题描述 投票:1回答:2

AppDelegate.m文件包含

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:taskID];
    }];
}

我不知道为什么我在gdb中收到此消息

无法endBackgroundTask:没有标识符为1fd57580的后台任务,或者它可能已经结束。在UIApplicationEndBackgroundTaskError()中断以进行调试。

ios xcode6
2个回答
3
投票

你的代码都错了。它应该是这样的:

UIBackgroundTaskIdentifier taskID = [application beginBackgroundTaskWithExpirationHandler:^{
    // Code to ensure your background processing stops executing
    // so it reaches the call to endBackgroundTask:
}];

// Put the code you want executed in the background here

if (taskID != UIBackgroundTaskInvalid) {
    [[UIApplication sharedApplication] endBackgroundTask:taskID];
}

1
投票

如果您在后台使用位置更新,请在获取用户的位置授权时添加以下代码。这是因为Apple从iOS 9开始将allowsBackgroundLocationUpdates的默认值更改为NO

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    locationManager.allowsBackgroundLocationUpdates = YES;
}
© www.soinside.com 2019 - 2024. All rights reserved.