OGeek|极客世界-中国程序员成长平台

标题: iOS 不可靠位置权限警报 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:03
标题: iOS 不可靠位置权限警报

在我们的应用中,我们要求在一个用于显示 map 的 View 上获得位置许可 (WhenInUse)。

如果用户选择禁用设备定位服务(即在设备设置中全局禁用)然后在应用中打开我们的 View ,将显示定位权限弹出窗口。重复冲洗几次(重新打开服务、继续应用、离开应用、关闭服务等),几次后位置权限警报将停止显示。

有人知道这是否是 iOS 中的错误(发生在 iOS 10 上)?

我们可以使用自己的警报来显示何时

CLLocationManager locationServicesEnabled = NO

但由于我们无法控制是否/何时弹出 iOS 位置警报,有时它们会同时显示,这是不好的用户体验。

该问题的任何已知解决方案?如果这是 iOS 中的错误,我必须向我们的 QA 和经理解释。

编辑:

- (BOOL)negotiateLocationServicePermissionUIViewController *)context
{
    /* Device location service is enabled. */
    if ([CLLocationManager locationServicesEnabled])
    {
        /* App location service is already authorized. */
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse
            || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways)
        {
            /* App location service is authorized. Start location updates ... */
            [self startUpdatingLocation];
            return YES;
        }
        else
        {
            /* App location service not yet authorized and status is not determined (aka: first time asking for permission). */
            if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
            {
                /* Request the location permission from the user. */
                if ([self respondsToSelectorselector(requestWhenInUseAuthorization)])
                    [self requestWhenInUseAuthorization]; /* iOS 8+ */
                else
                    [self startUpdatingLocation]; /* iOS 7 */
                return YES;
            }
            /* App location service not authorized and previously denied. */
            else
            {
                /* App location service permission was denied before. */
                // Show custom alert!
                return NO;
            }
        }
    }
    /* Device location service is disabled. */
    else
    {
        // Show custom alert!
        return NO;
    }
}



Best Answer-推荐答案


我不认为这是一个错误,这是不同的 AlertView。

如果用户接受一次位置权限,它会被保存,它不会再次询问他。但如果定位服务被禁用,情况就不同了。

你可以这样实现它:

if ([CLLocationManager locationServicesEnabled]){

    NSLog(@"Location Services Enabled");

    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
        alert = [[UIAlertView alloc] initWithTitle"App Permission Denied"     
                                           message"To re-enable, please go to Settings and turn on Location Service for this app." 
                                          delegate:nil 
                                 cancelButtonTitle"OK" 
                                 otherButtonTitles:nil];
        [alert show];
    }
}

因此,如果用户从设置中禁用定位服务,警报 View 可以重定向到设置页面。

这样就不会显示多个 AlertView。这仅取决于您希望如何处理每种情况,例如:

确保您处理每个案例并进行测试。

不知道我是否准确回答了您的问题,希望对您的实现有所帮助。

关于iOS 不可靠位置权限警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386047/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4