简体   繁体   English

iphone再次询问当前位置许可

[英]iphone ask for Current location permission again

Case: For current location, the user selected "Dont allow" on app install, so is there a way that I can ask again for the user location and trigger the native iphone alert for current location?? 案例:对于当前位置,用户在应用安装上选择“不允许”,那么有没有办法可以再次询问用户位置并触发当前位置的本机iphone警报?

I seen some posts on stackoverflow but there are old, is there a solution now to call in new sdk or someone found a way, 我在stackoverflow上看到了一些帖子,但是有旧的,现在有一个解决方案可以调用新的sdk或有人找到方法,

Post referred: CLLocation ask again for permission 帖子提到: CLLocation再次请求许可

Unfortunately you can't do that. 不幸的是你做不到。 One thing you can do is to prompt the user to change the location settings. 您可以做的一件事是提示用户更改位置设置。

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];
}

in ios8, apple has introduced a UIApplicationOpenSettingsURLString constant which is the location of the device's "settings" view. 在ios8中,apple引入了一个UIApplicationOpenSettingsURLString常量,它是设备“设置”视图的位置。

you can code the following (in swift) to direct the user to the settings view: 您可以编写以下代码(在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)
}

It seems the accepted answer is not completely true. 似乎接受的答案并非完全正确。 [CLLocationManager locationServicesEnabled] checks if the location services are enabled, as described in the documentation. [CLLocationManager locationServicesEnabled]检查是否已启用位置服务,如文档中所述。

Returns a Boolean value indicating whether location services are enabled on the device. 返回一个布尔值,指示是否在设备上启用了位置服务。 The user can enable or disable location services from the Settings application by toggling the Location Services switch in General. 用户可以通过切换“常规”中的“位置服务”开关,从“设置”应用程序启用或禁用位置服务。 You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. 您应该在开始位置更新之前检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。 Location services prompts users the first time they attempt to use location-related information in an app but does not prompt for subsequent attempts. 位置服务在用户第一次尝试在应用程序中使用与位置相关的信息时会提示用户,但不会提示后续尝试。 If the user denies the use of location services and you attempt to start location updates anyway, the location manager reports an error to its delegate. 如果用户拒绝使用位置服务并且您仍尝试启动位置更新,则位置管理器会向其委托报告错误。

If you want to check if user is given you permission to use his/her location, you should check [CLLocationManager authorizationStatus] . 如果要检查是否允许用户使用他/她的位置,则应检查[CLLocationManager authorizationStatus] If status for your app is kCLAuthorizationStatusDenied , it means user explicitly denied your app when it asked for the permission. 如果您的应用的状态为kCLAuthorizationStatusDenied ,则表示用户在请求权限时明确拒绝了您的应用。 You can use this and inform the user accordingly. 您可以使用它并相应地通知用户。

I don't think there is way to ask location permission again. 我认为没有办法再次询问位置许可。 But if you really need user location then you can display alert instructing them to enable it from settings. 但是,如果您确实需要用户位置,那么您可以显示警告,指示他们从设置中启用它。

This is what I did considering the previous answers : (this is in MonoTouch C# but easily translatable to Swift or Obj-C) 这就是我在考虑之前的答案时所做的:(这是在MonoTouch C#中,但很容易转换为Swift或Obj-C)

The following ask for permission and then proceed to location update if granted. 以下是要求许可,然后在获得批准后继续进​​行位置更新。 Otherwise, the next time the user will come; 否则,下次用户会来; if location services are disabled or denied it will display a message to request the permission / activation with a redirection to the settings 如果禁用或拒绝位置服务,它将显示一条消息,通过重定向到设置来请求权限/激活

//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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM