我有一个通过Visits API监视位置的应用。所需的行为是,即使系统终止了应用程序之后,该应用程序仍可以接收事件并对其进行处理。该应用程序将在不会运行许多应用程序的设备上永久永久正常运行。在更实际的用例中,设备有多个正在运行的应用程序似乎已终止了应用程序,然后该应用程序并未因新的位置事件而被唤醒。我将尝试并显示尽可能多的实现细节,如果您需要其他帮助我解决问题的方法,请告诉我。这让我发疯! 预先感谢!
应用程序委托:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let locationKey = launchOptions?[UIApplicationLaunchOptionsKey.location] as? NSDictionary
if locationKey != nil {
let locationManager = LocationService.locationInstance.locationManager
locationManager.delegate = LocationService.locationInstance
locationManager.requestAlwaysAuthorization()
sendTestNotification()
print(locationKey)
} else {
print("normal launch")
}
这是用户通过按钮启动跟踪时调用的函数:
class LocationService: NSObject, CLLocationManagerDelegate,UNUserNotificationCenterDelegate {
static let locationInstance = LocationService()
var locationManager = CLLocationManager()
func startTracking() {
notifications()
locationManager.delegate = LocationService.locationInstance
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringVisits()
locationManager.allowsBackgroundLocationUpdates = true
}