【问题标题】:How can I prompt the user to turn on location services i.e Go to Settings → Privacy → Location Services如何提示用户打开位置服务,即转到设置→隐私→位置服务
【发布时间】:2015-10-04 08:47:22
【问题描述】:

以下代码不再起作用,我们该怎么办

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]];

以下适用于转到设置→您的应用程序→位置,但我想访问转到设置→隐私→位置服务

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

我遇到了与以下问题相同的问题

Open Settings app from another app programmatically in iPhone

【问题讨论】:

    标签: ios settings cllocationmanager


    【解决方案1】:

    如果用户不开启定位服务,我会使用此代码

     - (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
         if ([[error domain] isEqualToString: kCLErrorDomain] && [error code] == kCLErrorDenied) {
           // The user denied your app access to location information.
    
           UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"This app does not have access to Location" message:@"You can enable access in Settings" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Settings", nil];
           alert.delegate = self;
           [alert show];
         }
    
    
        NSLog(@"%@", error.description);
    }
    

    并在此代码中处理UIAlertView 中的Settings 按钮:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
       NSLog(@"buttonIndex:%d",buttonIndex);
    
       if (buttonIndex == 1) {
           [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
       }
    }
    

    【讨论】:

    • 只有在用户拒绝使用定位服务后,用户才可以开启定位服务
    • 在这种情况下,在调用[self.locationManager requestWhenInUseAuthorization]; 应用程序后自动显示我的警报,并能够打开隐私设置
    • 如果用户误点击取消则
    猜你喜欢
    • 2011-06-20
    • 1970-01-01
    • 2016-10-05
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多