如何始终保持 iOS 应用程序进程 运行 尽管用户已终止应用程序并检查 iOS 中的设备位置?

How to keep an iOS app process run always though the user has terminated the app and check for device location in iOS?

我需要每隔一小时左右检查一次设备位置,如果该位置在特定区域之外(比如设备已被带出办公场所),请采取一些措施(比如显示本地通知说"Hey! The device is outside the office")。

为此,我需要每隔一小时检查一次位置,即使该应用已被终止。如何让应用程序在被用户终止的情况下永远保持活跃。

是的,你可以做到,但如果你的系统部署目标大于 7.0。

在ios7.1 中有一个名为startMonitoringSignificantLocationChanges 的方法。此方法在后台更新位置,即使根据苹果文档终止应用程序也是如此:

Starts the generation of updates based on significant location changes.

If you start this service and your app is subsequently terminated, the system automatically relaunches the app into the background if a new event arrives. In such a case, the options dictionary passed to the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods of your app delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your app was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

我找到了一个演示可能对您有所帮助。看这个 link http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

感谢您的回答。最好的方法是使用 Geofencing.

它使用 startMonitoringForRegion:(CLRegion *)region 提供区域中心的纬度和经度以及区域的半径。

当设备进入这个区域时,didEnterRegion 被调用。 当设备移出该区域时,将调用 didExitRegion。

是的 即使应用程序 terminated/backgrounded 也能正常工作。

此外,我们必须在应用程序的 Info.plist 文件中设置 NSLocationAlwaysUsageDescription,您可以在支持文件下找到该文件。

查看本文了解详细信息。 http://www.devfright.com/how-to-use-the-clregion-class-for-geofencing/

为了在模拟器中对此进行测试,我在 didEnterRegion 和 didExitRegion 方法中使用 UILocalNotification 进入和退出区域时使用了本地通知。

`UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:1];
notification.alertBody = @"You have entered the region";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];`

现在更改 iOS 模拟器的自定义位置(调试 -> 位置 -> 自定义位置),并提供区域内的纬度和经度,你会收到一条通知说 "You have entered the region" 并更改自定义位置到区域外的经纬度,你会收到一条通知说 "You have exited the region".