更新时间:2021-11-17 21:51:27
要在后台获取位置,请使用以下代码.每次重新启动后台任务,都会使您的应用在后台长时间运行.
To get a location in the background, use the following code. It will make your app run in the background for a long time by restarting the background task everytime.
要使用此功能,您需要使用 Background Fetch 和 Location Updates 在项目设置的 Capabilities 中打开 Background Mode . /strong>已打开.
To use this, you need to turn on Background Mode in Capabilities in project settings with Background Fetch and Location Updates turned on.
- (void)applicationDidEnterBackground:(UIApplication *)application {
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
[application endBackgroundTask:background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
}
}
}