简体   繁体   中英

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??

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,

Post referred: CLLocation ask again for permission

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.

you can code the following (in swift) to direct the user to the settings view:

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.

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] . If status for your app is kCLAuthorizationStatusDenied , it means user explicitly denied your app when it asked for the permission. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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