简体   繁体   English

持续位置更新后台iOS13

[英]Continuous location update background iOS13

I am currently testing the background location mode in iOS 13, as I want to track the user´s location and motion (using the CMMotionManager) in the background.我目前正在 iOS 13 中测试后台定位模式,因为我想在后台跟踪用户的位置和运动(使用 CMMotionManager)。 Therefore, I have my own (singleton) class handling the location tracking.因此,我有自己的(单例)class 处理位置跟踪。 I initialize the CLLocationManager in the following way:我通过以下方式初始化 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
    }

Then I start the following services:然后我启动以下服务:

   func startLocationServices() {
        // ...
        locationManager.startUpdatingLocation()
        locationManager.startMonitoringVisits()
        locationManager.startMonitoringSignificantLocationChanges()
        // ...
    }

In addition, I implemented the CLLocationManagerDelegate-methods didChangeAuthorization(), didUpdateLocation().另外,我实现了CLLocationManagerDelegate-methods didChangeAuthorization()、didUpdateLocation()。

In the info.plist-file I appended the following entries:在 info.plist 文件中,我附加了以下条目:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array> 

In my ViewController, I call the startLocationServices.在我的 ViewController 中,我调用 startLocationServices。 Currently, I set the app's authorization to track location data to ".authorizedAlways"目前,我将应用程序跟踪位置数据的授权设置为“.authorizedAlways”

The location updates stop after approximately 60 - 130 minutes.位置更新在大约 60 - 130 分钟后停止。

To solve the problem, I already added the didFinishLaunchingWithOptions-function, which triggers the location-updates again:为了解决这个问题,我已经添加了 didFinishLaunchingWithOptions 函数,它会再次触发位置更新:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let launchOptions = launchOptions,
        let isLocationKey = launchOptions[UIApplication.LaunchOptionsKey.location] as? Bool,
        isLocationKey {
        restartServices()

    }
    return true
} 

When the app gets awoken using this function, I managed to get continuous data on some tests, but sometimes the app was suspended again after a few minutes.当使用此 function 唤醒应用程序时,我设法获得了一些测试的连续数据,但有时应用程序会在几分钟后再次暂停。

Last, I also tried a timer that restarts the location tracking every 5 minutes, but this does not seem to affect the update-duration at all.最后,我还尝试了一个计时器,它每 5 分钟重新启动一次位置跟踪,但这似乎根本不会影响更新持续时间。

So my question is if there is a way of continuously receiving the location updates in the background, or is there some option I am missing?所以我的问题是,是否有办法在后台连续接收位置更新,或者我是否缺少一些选项?

Thanks in advance.提前致谢。

Edit: I tested the application on iOS 12 and it gets continuous updates in 5/5 tests.编辑:我在 iOS 12 上测试了该应用程序,它在 5/5 测试中得到持续更新。 So I guess the problem is related to iOS 13.所以我猜这个问题与 iOS 13 有关。

Nothing is wrong with code,!代码没有错,! , I have faced the same issue and after research I found that ,我遇到了同样的问题,经过研究我发现

At the WWDC19 keynote Apple announced two changes in the way location permissions will work in iOS 13. The first change gives users the option to share their location with your app just once.在 WWDC19 主题演讲中,Apple 宣布了 iOS 13 中位置权限工作方式的两项更改。第一项更改使用户可以选择仅与您的应用程序共享他们的位置一次。 This makes it easier to try out location features and helps users keep sensitive location data private.这样可以更轻松地尝试位置功能,并帮助用户将敏感的位置数据保密。

The first notable change is that even when you call requestAlwaysAuthorization , the user will only get the 'just now' and 'when in use' options in the permission dialog.第一个值得注意的变化是,即使您调用requestAlwaysAuthorization ,用户也只会在权限对话框中获得“刚刚”和“使用时”选项。 If the user grants you 'when in use' permission and you try to scan for location in the background, only then the user will be presented a dialog to grant the background permission.如果用户授予您“使用时”权限,并且您尝试在后台扫描位置,则只有这样,用户才会看到一个对话框以授予后台权限。

So When user grants WhenInUseUsage permission You will get always in CLAuthorizationStatus and If user choose Allow Once CLAuthorizationStatus will be rested to notDetermined once app launches again因此,当用户授予WhenInUseUsage权限时,您将always处于CLAuthorizationStatus ,如果用户选择Allow Once ,则应用程序再次启动后, CLAuthorizationStatus将恢复为notDetermined

You can check this article for detailed info https://medium.com/q42-engineering/apple-location-permission-ios13-1e0e59002889您可以查看这篇文章以获取详细信息https://medium.com/q42-engineering/apple-location-permission-ios13-1e0e59002889

And Here is video https://developer.apple.com/videos/play/wwdc2019/705/这是视频https://developer.apple.com/videos/play/wwdc2019/705/

EDIT编辑

After When In User permission granted by the user iOS will show user a another dialogue after some days to change permission from when in use to always allow. After When In User 用户授予的权限 iOS 将在几天后向用户显示另一个对话框,以将权限从使用时更改为始终允许。

So now there is no way to directly ask user for always allow permission instantly like we do before.所以现在没有办法像我们以前那样直接要求用户总是立即允许权限。

在此处输入图像描述

This is an iOS 13 bug.这是 iOS 13 错误。 The app permissions get messed up by iOS.应用程序权限被 iOS 搞砸了。 While it shows "Always" in settings, it actually behaves more like "While in Use".虽然它在设置中显示“始终”,但实际上它的行为更像“使用时”。 One app is put to sleep in background while other keeps getting locations on the same device (in background).一个应用程序在后台进入睡眠状态,而其他应用程序不断在同一设备上获取位置(在后台)。

To solve it, goto Settings-> Search your app with issue -> Location -> Change Always to Never and then back to Always.要解决它,请转到设置-> 搜索有问题的应用程序-> 位置-> 将始终更改为从不,然后返回始终。

Unfortunately, there is nothing, you can do in the code to fix this.不幸的是,没有什么,您可以在代码中解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM