iOS - 使用设置和取消按钮打开定位服务 - 如何捕获取消按钮单击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21334780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:14:54  来源:igfitidea点击:

iOS - Turn on Location Services with Settings and Cancel buttons -How do I Capture Cancel button click

ioscllocationmanager

提问by DigitalMafiaPro

When the user launches the app for the first time and attempts to login, they are prompted with the iOS dialog - "Turn On Location Services".

当用户第一次启动应用程序并尝试登录时,系统会提示他们使用 iOS 对话框 -“打开定位服务”。

I need to capture when the user clicks "cancel". Is there a Notificationsent? If so, what is its name? I've been unable to locate it.

我需要在用户单击“取消”时进行捕获。有Notification寄吗 如果有,它的名字是什么?我一直无法找到它。

The CLAUthorizationStatusis kCLAuthorizationDeniedwhen Location Services are Disabled OR the user clicked "Don't allow". When the user clicks "Cancel", it does not fire the authorizationChangeevent. When user clicks "Cancel", the app just hangs.

CLAUthorizationStatuskCLAuthorizationDenied,当位置服务被禁用或用户点击“不允许”。当用户单击“取消”时,它不会触发该authorizationChange事件。当用户单击“取消”时,应用程序就会挂起。

回答by furins

Short answer: You can't catch that notification. You can infer about the user choice and act consequently by using CLLocationManagermethods (the longer answer below).

简短回答:您无法捕捉到该通知。您可以通过使用CLLocationManager方法(下面较长的答案)来推断用户的选择并因此采取行动。

Longer answer:

更长的答案:

Firstly, welcome on Stack Overflow. Before kindly posing your question, and trying to be collaborative with people that are here to help, it's a good idea to search if somebody else previously posed the same question.

首先,欢迎使用 Stack Overflow。在善意地提出您的问题并尝试与在这里提供帮助的人合作之前,最好先搜索一下其他人之前是否提出过同样的问题。

A brief search gave (just to mention some of them):

进行了简短的搜索(仅提及其中一些):

Now, let's try to summarize them all, starting from iOS docs:

现在,让我们尝试从 iOS 文档开始总结它们:

If your app relies on location services to function properly, you should include the UIRequiredDeviceCapabilities key in the app's Info.plist file. You use this key to specify the location services that must be present in order for your app to run. The App Store uses the information in this key from preventing users from downloading apps to devices that do not contain the listed features.

Important: If your app uses location services but is able to operate successfully without them, do not include the corresponding strings in the UIRequiredDeviceCapabilities key.

如果您的应用依赖位置服务才能正常运行,则您应该在应用的 Info.plist 文件中包含 UIRequiredDeviceCapabilities 键。您使用此键指定必须存在的位置服务,以便您的应用程序运行。App Store 使用此密钥中的信息阻止用户将应用程序下载到不包含所列功能的设备。

重要提示:如果您的应用程序使用位置服务但无需它们也能成功运行,请不要在 UIRequiredDeviceCapabilities 键中包含相应的字符串。

So, if your app really needs to access the user's position you should add location-servicesand eventually gpsto UIRequiredDeviceCapabilities.

因此,如果您的应用确实需要访问用户的位置,您应该添加location-services并最终添加gpsUIRequiredDeviceCapabilities.

Then, somewhere in your code - when needed, you have to check if the location services are enabled.

然后,在您的代码中的某处 - 需要时,您必须检查位置服务是否已启用。

[CLLocationManager locationServicesEnabled]

they may be disallowed for three reasons:

他们可能因以下三个原因而被禁止:

  • The user can disable location services in the Settings app.
  • The user can deny location services for a specific app.
  • The device might be in Airplane mode and unable to power up the necessary hardware.
  • 用户可以在“设置”应用中禁用定位服务。
  • 用户可以拒绝特定应用程序的位置服务。
  • 设备可能处于飞行模式,无法启动必要的硬件。

You are interested in the second case: the user refused to allow your app to use the location services.

您对第二种情况感兴趣:用户拒绝允许您的应用使用位置服务。

Again, from the docs:

再次,来自文档:

Important: In addition to hardware not being available, the user has the option of denying an application's access to location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. You can also check the application's explicit authorization status using the authorizationStatus method.

重要提示:除了硬件不可用之外,用户还可以选择拒绝应用程序访问位置服务数据。在应用程序初始使用期间,Core Location 框架会提示用户确认使用位置服务是可以接受的。如果用户拒绝请求,CLLocationManager 对象会在未来的请求中向其委托报告适当的错误。您还可以使用 authorizationStatus 方法检查应用程序的显式授权状态。

[CLLocationManager authorizationStatus]

That may return:

那可能会返回:

  • kCLAuthorizationStatusNotDeterminedif the user has not yet made a choice regarding whether this application can use location services.
  • kCLAuthorizationStatusRestrictedthis application is not authorized to use location services. The user cannot change this application's status, possibly due to active restrictions such as parental controls being in place.
  • kCLAuthorizationStatusDeniedThe user explicitly denied the use of location services for this application or location services are currently disabled in Settings.
  • kCLAuthorizationStatusAuthorizedThis application is authorized to use location services.
  • kCLAuthorizationStatusNotDetermined如果用户尚未选择此应用程序是否可以使用位置服务。
  • kCLAuthorizationStatusRestricted此应用程序无权使用定位服务。用户无法更改此应用程序的状态,这可能是由于设置了家长控制等活动限制。
  • kCLAuthorizationStatusDenied用户明确拒绝为此应用程序使用定位服务,或者定位服务当前在“设置”中被禁用。
  • kCLAuthorizationStatusAuthorized此应用程序被授权使用位置服务。

If[CLLocationManager locationServicesEnabled]returns NOand you attempt to start location services anyway (i.e. calling [locationManager startUpdatingLocation]), the system prompts the user to confirm whether location services should be re-enabled. Given that location services are very likely to be disabled on purpose, the user might not welcome this prompt.

如果[CLLocationManager locationServicesEnabled]返回NO并且您仍然尝试启动定位服务(即调用[locationManager startUpdatingLocation]),系统会提示用户确认是否应重新启用定位服务。考虑到定位服务很可能被故意禁用,用户可能不欢迎这个提示。

I suppose you know, and did all the previous steps (I'm only sure you checked the authorizationStatus).You refused to show us the significant code of your app so I can only suppose the overall logic behind. Now you said your app hangs. This should be because you didn't catch the error properly? Catching the error is the way to re-prompt the user, if you wish.

我想你知道,并完成了之前的所有步骤(我只确定你检查了authorizationStatus)。你拒绝向我们展示你的应用程序的重要代码,所以我只能假设背后的整体逻辑。现在你说你的应用挂了。这应该是因为您没有正确捕获错误?如果您愿意,捕获错误是重新提示用户的方法。

After calling [locationManager startUpdatingLocation], if not authorized, your delegate should define a locationManager:didFailWithError:in order to catch the kCLErrorDenied.

调用后[locationManager startUpdatingLocation],如果未授权,您的委托应定义一个locationManager:didFailWithError:以捕获kCLErrorDenied

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

You may show, at this point, a UIAlertto insist asking the user to give you access to its position, or trigger a change in the UI or whatever you like.

在这一点上,您可以显示 a UIAlertto 坚持要求用户让您访问其位置,或者触发 UI 或您喜欢的任何更改。



Final notes最后的笔记

I hope you understand why I was asking for the code: the reason was to offer you an alternative solution instead of reply "You can't catch the 'Cancel' notification".

我希望你明白我为什么要代码:原因是为你提供一个替代解决方案,而不是回复“你无法捕捉到‘取消’通知”。

If this answer does not satisfy your question please elaborate why you need to catch the pushing of the "Cancel"/"Do not allow" button, so we can provide alternatives.

如果此答案不能满足您的问题,请详细说明您为什么需要按下“取消”/“不允许”按钮,以便我们提供替代方案。

Clearly my advice is to not annoy people to death by continuously ask them for enabling location services if they don't want.

显然,我的建议是不要通过不断要求他们启用位置服务(如果他们不想要)来惹恼他们。

Post scriptum: Maybe that the answer looks pedantic and obvious in certain parts if not all to you, but we are here to provide answers also for future readers.

后记:也许答案在某些部分看起来很迂腐和显而易见,如果不是全部的话,但我们在这里也为未来的读者提供答案。