iPhone再次要求当前位置权限

8
案例:当前位置,用户在应用安装时选择了“不允许”,那么我是否有办法再次请求用户位置并触发iPhone本地警报以获取当前位置?我在stackoverflow上看到了一些帖子,但它们都比较旧,现在是否有新的SDK调用解决方案或者有人找到了方法?
参考帖子: CLLocation ask again for permission

2
如果用户选择“允许”或“拒绝”,仍然无法更改每个已安装应用程序的设置。 - adali
5个回答

10

很抱歉您无法这样做。您可以提示用户更改位置设置。

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

5
我已经做了这件事,但是客户想要他们脑海中所想的每一件事情 :| - Raheel Sadiq
很好 :) 更好的做法是向客户说明情况及其优势。 - manujmv

8
在iOS 8中,苹果引入了一个UIApplicationOpenSettingsURLString常量,该常量指向设备的“设置”视图。
您可以编写以下代码(使用Swift)将用户重定向到设置视图:
switch CLLocationManager.authorizationStatus() {
    case .AuthorizedWhenInUse, .Restricted, .Denied:
        let alertController = UIAlertController(
            title: "Background Location Access Disabled",
            message: "In order to be notified, please open this app's settings and set location access to 'Always'.",
            preferredStyle: .Alert)

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        alertController.addAction(cancelAction)

        let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
            if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
                UIApplication.sharedApplication().openURL(url)
            }
        }
        alertController.addAction(openAction)

        self.presentViewController(alertController, animated: true, completion: nil)
}

在添加“打开设置”操作之前,难道不需要检查系统版本吗? - Yohan Dahmani

5
似乎被接受的答案并不完全正确。如文档所述,[CLLocationManager locationServicesEnabled]检查位置服务是否已启用。

返回一个布尔值,指示设备上是否启用了位置服务。 用户可以通过在“通用”中切换“位置服务”开关来从“设置”应用程序启用或禁用位置服务。 在开始位置更新之前,您应该检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。当用户首次尝试在应用程序中使用与位置相关的信息时,位置服务会提示用户,但不会提示后续尝试。如果用户拒绝使用位置服务,并且您仍然尝试启动位置更新,则位置管理器会向其委托报告错误。

如果您想检查用户是否授权您使用他/她的位置,请检查[CLLocationManager authorizationStatus]。如果您的应用程序的状态是kCLAuthorizationStatusDenied,这意味着当您的应用程序请求权限时,用户明确拒绝了您的应用程序。您可以使用此功能相应地通知用户。

0
这是我考虑之前的答案所做的事情: (这是在MonoTouch C#中,但很容易转换为Swift或Obj-C)
以下内容会请求权限,如果被授权,则继续进行位置更新。否则,下次用户访问时,如果位置服务已禁用或拒绝,则会显示一个消息以请求权限/激活,并重定向到设置。
//Location Manager (foreground)
        CLLocationManager locMgr = new CLLocationManager();
        locMgr.PausesLocationUpdatesAutomatically = false;

        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            locMgr.RequestWhenInUseAuthorization();
        }

        if (CLLocationManager.LocationServicesEnabled && CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
        {
            //set the desired accuracy, in meters
            locMgr.DesiredAccuracy = 150;
            locMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
            {
                Console.WriteLine(e.Locations);
            };

            locMgr.StartUpdatingLocation();
        }
        else if (!CLLocationManager.LocationServicesEnabled || CLLocationManager.Status == CLAuthorizationStatus.Denied)
        {
            var alert = UIAlertController.Create(NSBundle.MainBundle.LocalizedString("Location access", "Location access"), NSBundle.MainBundle.LocalizedString("Please check location", "To show your position on the map, you have to enable location services and authorize the app"), UIAlertControllerStyle.Alert);

            alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Cancel, null));

            if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
            {
                alert.AddAction(UIAlertAction.Create(NSBundle.MainBundle.LocalizedString("Go to settings", "Go to settings"), UIAlertActionStyle.Default, delegate
                {

                    var url = new NSUrl(UIApplication.OpenSettingsUrlString);
                    UIApplication.SharedApplication.OpenUrl(url);

                }));
            }

            PresentViewController(alert, true, null);
        }

0

我认为没有再次请求位置权限的方法。但是如果您确实需要用户位置,则可以显示警报,指示他们从设置中启用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接