简体   繁体   English

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

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

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 Notification sent? 是否发送了Notification If so, what is its name? 如果是这样,它的名字是什么? I've been unable to locate it. 我一直无法找到它。

The CLAUthorizationStatus is kCLAuthorizationDenied when Location Services are Disabled OR the user clicked "Don't allow". 当位置服务被禁用或用户单击“不允许”时, CLAUthorizationStatuskCLAuthorizationDenied When the user clicks "Cancel", it does not fire the authorizationChange event. 当用户单击“取消”时,它不会触发authorizationChange事件。 When user clicks "Cancel", the app just hangs. 当用户点击“取消”时,应用程序就会挂起。

Short answer: You can't catch that notification. 简短的回答:你无法抓住那个通知。 You can infer about the user choice and act consequently by using CLLocationManager methods (the longer answer below). 您可以通过使用CLLocationManager方法推断用户选择并采取相应措施(下面的答案越长)。

Longer answer: 更长的回答:

Firstly, welcome on Stack Overflow. 首先,欢迎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. 在善意提出问题并尝试与在场帮助的人合作之前,最好先搜索其他人之前提出的相同问题。

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. 如果您的应用依赖于位置服务来正常运行,您应该在应用的Info.plist文件中包含UIRequiredDeviceCapabilities键。 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. App Store使用此密钥中的信息阻止用户将应用程序下载到不包含所列功能的设备。

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. 重要提示:如果您的应用程序使用位置服务但能够在没有它们的情况下成功运行,请不要在UIRequiredDeviceCapabilities键中包含相应的字符串。

So, if your app really needs to access the user's position you should add location-services and eventually gps to 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. 在应用程序初次使用期间,Core Location框架会提示用户确认使用位置服务是可以接受的。 If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. 如果用户拒绝该请求,则CLLocationManager对象会在将来的请求期间向其委托报告相应的错误。 You can also check the application's explicit authorization status using the authorizationStatus method. 您还可以使用authorizationStatus方法检查应用程序的显式授权状态。

[CLLocationManager authorizationStatus]

That may return: 这可能会回归:

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

If [CLLocationManager locationServicesEnabled] returns NO and you attempt to start location services anyway (ie calling [locationManager startUpdatingLocation] ), the system prompts the user to confirm whether location services should be re-enabled. 如果[CLLocationManager locationServicesEnabled]返回NO并且您无论如何都尝试启动位置服务(即调用[locationManager startUpdatingLocation] ),系统会提示用户确认是否应重新启用位置服务。 Given that location services are very likely to be disabled on purpose, the user might not welcome this prompt. 鉴于位置服务很可能是故意禁用的,用户可能不欢迎此提示。

I suppose you know, and did all the previous steps (I'm only sure you checked the authorizationStatus ). 我想你知道,并做了所有前面的步骤(我只确定你检查了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. 如果您愿意,捕获错误是重新提示用户的方法。

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 UIAlert to insist asking the user to give you access to its position, or trigger a change in the UI or whatever you like. 此时,您可以显示UIAlert坚持要求用户授予您访问其位置的权限,或触发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. Post scriptum:如果不是全部的话,也许答案在某些部分看起来很迂腐和明显,但我们也在这里为未来的读者提供答案。

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

相关问题 当我的应用程序中的位置服务关闭时,如何显示包含设置+取消按钮的UIAlertView? - How to present UIAlertView which contains Settings + Cancel button when location services are turned off in my app? 如何在iOS应用中撤消/取消/注销“位置服务” - How to Revoke / Cancel / Unregister “Location Services” inside of the iOS app 如何处理“警报”中针对位置服务弹出的“取消”按钮 - How to handle “Cancel” button on Alert pop up for Location services 处理取消位置服务警报 - Handling cancel of the location services alert iOS LAPolicyDeviceOwnerAuthentication“取消”按钮 - iOS LAPolicyDeviceOwnerAuthentication “Cancel” Button 如何在iOS中取消后台阻止操作? - How do i cancel a background block operation in iOS? 在特定时间后如何取消iOS本地通知? - How do I cancel an iOS local notification after a particular time? 如何在 iOS 警报中显示不同的取消颜色? - How do I show different color for cancel in iOS alert? 如何更改UISearchbar取消按钮的文本颜色 - How do I change the UISearchbar cancel button's text color 如何在UIAlertView(iOS)中的其他两个按钮(堆叠)之间添加“取消”按钮 - How to add Cancel button between two other buttons (stacked) in UIAlertView (iOS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM