Objective C - CLLocationManager 找出何时“允许”或“不允许”被点击
在执行 CLLocationManager 时,是否有一个委托方法会在用户单击请求使用位置的“允许”或“不允许”提示时被调用?
我尝试了此操作,但在用户“允许”或“不允许”后不会调用此操作。
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
另外,是否有一个变量可以告诉我用户选择了什么?
我尝试了下面的方法,但总是返回 true。
locationManager.locationServicesEnabled
谢谢你,
球座
When doing CLLocationManager, is there a delegate method that gets called when a user clicked the "Allow" or "Don't allow" prompt that request to use Location?
I tried this but this doesn't get called after a user "Allow" or "Don't allow".
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
Also, is there a variable that will tell me what the user selected?
I tried the below, but that always returns true.
locationManager.locationServicesEnabled
Thank you,
Tee
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个委托方法
There is a delegate method for that
[CLLocationManager locationServicesEnabled]
仅告诉您设备上是否启用了位置服务。[CLLocationManagerauthorizationStatus]
返回您要查找的实际状态。[CLLocationManager locationServicesEnabled]
only tells your if the location service are enabled on the device.[CLLocationManager authorizationStatus]
returns the actual status you're looking for.您必须实现
didFailWithError:
方法:编辑: 查看 CLLocationManager 的参考资料,我发现了这一点:
You'll have to implement
didFailWithError:
method:EDIT: Looking at CLLocationManager's reference I've found this:
locationManager.locationServicesEnabled
指示位置服务是否可用,但不一定意味着您的应用允许使用它们。如果需要查找某个时间点的状态,请使用 CLLocationManager.authorizationStatus(),或实现
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
请注意,从 iOS 8 开始,当您的应用程序首次尝试使用位置服务时,授权请求不会自动发生。在对
CLLocationManager
实例调用startUpdatingLocation()
之前,您需要显式调用requestWhenInUseAuthorization()
。并确保您在 Info.plist 中具有
NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
键,具体取决于您所追求的授权类型。如果缺少这些,则不会有任何错误、日志、提示,也不会为您指明正确的方向:)locationManager.locationServicesEnabled
indicates whether location services is available, but not necessarily mean they are allowed for your app.Use
CLLocationManager.authorizationStatus()
if you need to find out the status at a point in time, or implement- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
Take note since iOS 8, the authorization request does not takes place automatically when your application first attempts to use location services. You need to call
requestWhenInUseAuthorization()
explicitly before you callstartUpdatingLocation()
on yourCLLocationManager
instance.And make sure you have the
NSLocationAlwaysUsageDescription
orNSLocationWhenInUseUsageDescription
key in Info.plist, depending on the type of authorization you are after. If these are missing, there are no errors, no logs, no hints, no nothing that will point you in the right direction :)