Objective C - CLLocationManager 找出何时“允许”或“不允许”被点击

发布于 2024-12-07 05:13:11 字数 397 浏览 0 评论 0原文

在执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

垂暮老矣 2024-12-14 05:13:11

有一个委托方法

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        // user allowed
    }

}

There is a delegate method for that

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
        // user allowed
    }

}
真心难拥有 2024-12-14 05:13:11

[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.

困倦 2024-12-14 05:13:11

您必须实现 didFailWithError: 方法:

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

    if ([error domain] == kCLErrorDomain) {

        // We handle CoreLocation-related errors here
        switch ([error code]) {
        // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
        // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
        case kCLErrorDenied:

        case kCLErrorLocationUnknown:

        default:
            break;
        }

    } else {
    // We handle all non-CoreLocation errors here
    }
}

编辑: 查看 CLLocationManager 的参考资料,我发现了这一点:

+ (CLAuthorizationStatus)authorizationStatus

返回值
指示应用程序是否被授权的值
使用定位服务。

讨论管理给定应用程序的授权状态
由系统并由多种因素决定。申请必须是
用户明确授权使用定位服务,并且
当前必须为系统启用位置服务本身。
当您申请时,此授权会自动发生
首次尝试使用定位服务。

You'll have to implement didFailWithError: method:

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

    if ([error domain] == kCLErrorDomain) {

        // We handle CoreLocation-related errors here
        switch ([error code]) {
        // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
        // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
        case kCLErrorDenied:

        case kCLErrorLocationUnknown:

        default:
            break;
        }

    } else {
    // We handle all non-CoreLocation errors here
    }
}

EDIT: Looking at CLLocationManager's reference I've found this:

+ (CLAuthorizationStatus)authorizationStatus

Return Value
A value indicating whether the application is authorized
to use location services.

Discussion The authorization status of a given application is managed
by the system and determined by several factors. Applications must be
explicitly authorized to use location services by the user and
location services must themselves currently be enabled for the system.
This authorization takes place automatically when your application
first attempts to use location services.

廻憶裏菂餘溫 2024-12-14 05:13:11

locationManager.locationServicesEnabled 指示位置服务是否可用,但不一定意味着您的应用允许使用它们。

如果需要查找某个时间点的状态,请使用 CLLocationManager.authorizationStatus(),或实现

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

请注意,从 iOS 8 开始,当您的应用程序首次尝试使用位置服务时,授权请求不会自动发生。在对 CLLocationManager 实例调用 startUpdatingLocation() 之前,您需要显式调用 requestWhenInUseAuthorization()

并确保您在 Info.plist 中具有 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键,具体取决于您所追求的授权类型。如果缺少这些,则不会有任何错误、日志、提示,也不会为您指明正确的方向:)

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 call startUpdatingLocation() on your CLLocationManager instance.

And make sure you have the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文