小编mat*_*tll的帖子

连续位置更新后台iOS13

我目前正在 iOS 13 中测试后台位置模式,因为我想在后台跟踪用户的位置和动作(使用 CMMotionManager)。因此,我有自己的(单例)类来处理位置跟踪。我通过以下方式初始化 CLLocationManager:

    func initializeLocationManager() -> CLLocationManager {
        let manager = locationManager ?? CLLocationManager()
        manager.delegate = self
        manager.requestAlwaysAuthorization()
        manager.allowsBackgroundLocationUpdates = true
        manager.pausesLocationUpdatesAutomatically = false
        manager.distanceFilter = kCLDistanceFilterNone
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.activityType = .other
        return manager
    }
Run Code Online (Sandbox Code Playgroud)

然后我启动以下服务:

   func startLocationServices() {
        // ...
        locationManager.startUpdatingLocation()
        locationManager.startMonitoringVisits()
        locationManager.startMonitoringSignificantLocationChanges()
        // ...
    }
Run Code Online (Sandbox Code Playgroud)

此外,我还实现了 CLLocationManagerDelegate 方法 didChangeAuthorization()、didUpdateLocation()。

在 info.plist 文件中,我附加了以下条目:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array> 
Run Code Online (Sandbox Code Playgroud)

在我的 ViewController 中,我调用了 startLocationServices。目前,我将应用跟踪位置数据的授权设置为“.authorizedAlways”

位置更新在大约 60 - 130 分钟后停止。

为了解决这个问题,我已经添加了 didFinishLaunchingWithOptions 函数,它会再次触发位置更新:

func application(_ …
Run Code Online (Sandbox Code Playgroud)

background cllocationmanager cllocation ios swift

12
推荐指数
1
解决办法
5499
查看次数

标签 统计

background ×1

cllocation ×1

cllocationmanager ×1

ios ×1

swift ×1