在iOS 14中处理位置权限

Apple is without a doubt the leader in data privacy. Location access is something that’s been wrongfully used or rather abused in the past by various apps. It’s a security threat — or rather a breach — and with iOS 14, Apple once again looks to give users better control of the data they’re sharing.

毫无疑问,苹果是数据隐私的领导者。 过去,位置访问是各种应用程序曾经错误地使用或滥用的位置。 这是一种安全威胁-或更确切地说是一种破坏-在iOS 14上,Apple再次希望为用户提供对共享数据的更好控制。

iOS 14 brings a slight change to the CoreLocation framework. Going forward, users can choose whether to give precise or approximate location access.

iOS 14对CoreLocation框架进行了少许更改。 展望未来,用户可以选择是提供精确定位还是粗略定位访问。

Before we see how to manage the new iOS 14 location changes, let’s quickly recap what was new in iOS 13 location permissions.

在看到如何管理新的iOS 14位置更改之前,让我们快速回顾一下iOS 13位置权限中的新增功能。

iOS 13位置权限快速回顾 (Quick Recap of iOS 13 Location Permissions)

Last year, in iOS 13, Apple changed the way location tracking permissions work.

去年,在iOS 13中,Apple更改了位置跟踪权限的工作方式。

  • Notably, there was a new “Allow Once” permission that required setting NSLocationWhenInUseUsageDescription. It’s important to note that this permission gets automatically revoked when the app is closed.

    值得注意的是,有一个新的“允许一次”权限,需要设置NSLocationWhenInUseUsageDescription 。 重要的是要注意,关闭应用程序后,此权限会自动被撤销。

  • Also, enabling “Allow While Using The App” would provisionally “Always allow” location tracking. Now, when you try accessing a location in the background, the system presents a dialog to the user to let the app continue tracking it or deny it.

    另外,启用“在使用应用程序时允许”将临时“始终允许”位置跟踪。 现在,当您尝试在后台访问某个位置时,系统会向用户显示一个对话框,让应用程序继续跟踪或拒绝它。
  • iOS 13.4 introduced a better approach to quickly ensure that “Always allow” permission is granted. Simply ask for the authorizedWhenInUse, and if it’s granted, show a prompt for authorizedAlways.

    iOS 13.4引入了一种更好的方法来快速确保授予“始终允许”权限。 只需索取authorizedWhenInUse ,如果被authorizedWhenInUse ,则提示输入authorizedAlways

For an in-depth look at handling iOS 13 location permissions in your applications, refer to this article.

有关在应用程序中处理iOS 13位置权限的详细信息, 请参阅本文

In the following section, we’ll see how to manage location changes in an iOS 14 SwiftUI application.

在以下部分中,我们将看到如何在iOS 14 SwiftUI应用程序中管理位置更改。

Let’s get started.

让我们开始吧。

iOS 14 CoreLocation更改 (iOS 14 CoreLocation Changes)

Apple has deprecated the class function authorizationStatus() that we invoked earlier on the CLLocationManager.

苹果已经弃用了我们先前在CLLocationManager上调用的类函数authorizationStatus()

This means that starting in iOS 14, authorizationStatus() can be only called on the instance of CLLocationManager.

这意味着从iOS 14开始,只能在CLLocationManager的实例上调用authorizationStatus()

Apple has also deprecated the didChangeAuthorization delegate function of CoreLocation that contained a status argument. Instead, we now have a new locationManagerDidChangeAuthorization function.

苹果还淘汰了包含status参数的CoreLocationdidChangeAuthorization委托函数。 相反,我们现在有了一个新的locationManagerDidChangeAuthorization函数。

Image for post

To determine the location accuracy status, we can invoke the new enum property accuracyAuthorization on the location manager instance. This property is of the type CLAccuracyAuthorization and has two enum cases:

要确定位置准确性状态,我们可以在位置管理器实例上调用新的枚举属性accuracyAuthorization 。 此属性的类型为CLAccuracyAuthorization并且具有两种枚举情况:

  • fullAccuracy

    fullAccuracy

  • reducedAccuracy (returns an approximate instead of exact location)

    reducedAccuracy (返回一个近似值而不是精确位置)

Setting up CoreLocation for location updates is exactly the same as in iOS 13:

设置CoreLocation进行位置更新与iOS 13中的完全相同:

locationManager.delegate = self
locationManager.requestAlwaysAuthorization()locationManager.startUpdatingLocation()locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = false

Note: For allowsBackgroundLocationUpdates, ensure that you’ve enabled the Background mode location from the capabilities in your Xcode project.

注意:对于 allowsBackgroundLocationUpdates ,请确保已通过Xcode项目中的功能启用了背景模式位置。

Now, when you run the code above on your device, you’ll get the following revamped prompt in iOS 14:

现在,当您在设备上运行上面的代码时,您将在iOS 14中得到以下修改后的提示:

Image for post
Screenshot from the author’s phone.
作者电话的屏幕截图。

By toggling the “Precise” button, you can choose to allow approximate or accurate location access.

通过切换“精确”按钮,您可以选择允许近似或精确的位置访问。

Now, there could be a use case where you are required to only access a user’s accurate location.

现在,可能有一个用例,只要求您访问用户的准确位置。

Thankfully, there’s a new function in iOS 14 that lets us request that temporarily:

值得庆幸的是,iOS 14中有一个新功能,可以让我们临时请求该功能:

Image for post

The requestTemporaryFullAccuracyAuthorization function requires a purpose key to explain the need for an accurate location to the user. This key is defined inside info.plist file within a NSLocationTemporaryUsageDescriptionDictionary dictionary, as shown below:

requestTemporaryFullAccuracyAuthorization函数需要一个目的键来向用户解释对准确位置的需求。 该密钥在NSLocationTemporaryUsageDescriptionDictionary词典的info.plist文件中定义,如下所示:

Image for post

Once the requestTemporaryFullAccuracyAuthorization is invoked, the following prompt is displayed:

调用requestTemporaryFullAccuracyAuthorization ,将显示以下提示:

Image for post
Screenshot by the author.
作者的屏幕截图。

reducedAccuracy and fullAccuracy location updates are both received inside the didUpdateLocations delegate method.

reducedAccuracyfullAccuracy位置更新都在didUpdateLocations委托方法中接收。

The full source code for a sample iOS 14 CoreLocation application in SwiftUI is available on GitHub.

CoreLocation的示例iOS 14 CoreLocation应用程序的完整源代码可在GitHub上获得

It’s important to note that for background location updates with reducedAccuracy, the time interval of location updates isn’t changed. Also, beacons and region monitoring are disabled under reducedAccuracy.

重要的是要注意,对于背景reducedAccuracy后台位置更新,位置更新的时间间隔不会更改。 此外,信标和区域的监控下被禁用reducedAccuracy

AppClip,小部件和默认设置的CoreLocation更新 (CoreLocation Updates for AppClips, Widgets, and Default Settings)

AppClips are like mini-app modules that can run without installing the complete application.

AppClips类似于微型应用程序模块,无需安装完整的应用程序即可运行。

  • When you’re accessing a location within AppClips, there’s no “While Using App” permission. Instead, there’s a “While Using Until Tomorrow” permission that automatically gets reset at the end of the day.

    当您访问AppClips中的位置时,没有“使用应用程序时”权限。 取而代之的是,有一个“直到明天使用时”权限,该权限会在一天结束时自动重置。
  • For accessing a location inside WidgetKit, you need to define the NSWidgetWantsLocation key in the widget’s info.plist file.

    要访问WidgetKit中的位置,您需要在小部件的info.plist文件中定义NSWidgetWantsLocation键。

  • To only show a prompt for approximate location by default in your app, you can add the info.plist key NSLocationDefaultAccuracyReduced. By doing so, the precise location toggle button won’t be displayed in the permission dialog. But the user can still enable the toggle from the phone’s settings.

    要仅在应用程序中默认情况下仅显示提示提示您的位置,可以添加info.plistNSLocationDefaultAccuracyReduced 。 这样,精确位置切换按钮将不会显示在权限对话框中。 但是用户仍然可以从手机设置中启用切换。

结论 (Conclusion)

CoreLocation has brought an interesting change to iOS 14 that lets the user have more control over their location data. Not all apps require their exact location, so you can choose the reducedAccuracy property to fetch only the approximate location.

CoreLocation为iOS 14带来了有趣的变化,使用户可以更好地控制自己的位置数据。 并非所有应用程序都需要它们的确切位置,因此您可以选择reducedAccuracy属性仅获取大概位置。

That’s it for this one. Thanks for reading.

就这个。 谢谢阅读。

翻译自: https://medium.com/better-programming/handling-location-permissions-in-ios-14-2cdd411d3cca

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值