首页 > 解决方案 > 如何测试后台位置更新iOS?

问题描述

我有一个应用,需要用户在后台的位置来触发某些事件和通知,类似于 life360;如果用户终止应用程序,后台位置更新仍应发生。

我的清单:

<key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>location</string>
        <string>processing</string>
        <string>remote-notification</string>
    </array>

在收到“始终”位置权限后,我已经像这样配置了我的 CLLocationManager:

locationManager = CLLocationManager()
locationManager.delegate  = self
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.distanceFilter = kCLDistanceFilterNone  //meters
locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startMonitoringSignificantLocationChanges()
locationManager.startMonitoringVisits()

我已经设置了这样的 willFinishLaunching 函数来测试我的应用程序是否在位置更改时被召唤:

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    if launchOptions?[UIApplication.LaunchOptionsKey.location] != nil {
        // App launched from the background due to location change
        UserDefaults.standard.set("Background Update Received", forKey: "locationUpdate")  
    }
    

    return true
    
}

在过去的几个小时里,我一直在我的城市周围开车,我的应用程序被终止,试图触发位置更新,但它从未发生过。我的 iPhone 具有后台应用刷新功能和“始终”位置权限。我已经看到这个过程很乏味并且非常耗时。

我错过了什么吗?有没有比整天开车浪费汽油更好的方法来测试这个?

标签: iosswiftcllocationmanagercllocation

解决方案


推荐阅读