Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xamarin iOS: Location Request how to find out when user clicks “Allow” or “Don't allow” is clicked

When the app loads, user is prompted with enable location permission. I want to move to next page only when the user hits "allow" or "don't allow" on that popup.

I saw a few questions like this but they did not help.

My code:

var locationManager = new CLLocationManager();
locationManager.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => 
{
   if(ee.Status == CLAuthorizationStatus.AuthorizedAlways || ee.Status ==CLAuthorizationStatus.Denied)
    {
      //Goto next page
    }
    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
    {
        locationManager.DesiredAccuracy = CLLocation.AccuracyBest;
        locationManager.DistanceFilter = CLLocationDistance.FilterNone;
        locationManager.RequestAlwaysAuthorization();
    }
}

AuthorizationChanged is prompted when location dialog pops up and the status is always CLAuthorizationStatus.NotDetermined and unable to detect when user clicks "allow" or "don't allow".

like image 666
TheDeveloper Avatar asked Oct 26 '25 03:10

TheDeveloper


1 Answers

When the app loads, user is prompted with enable location permission. I want to move to next page only when the user hits "allow" or "don't allow" on that popup.

In your ViewDidLoad method you can do something like this:

// showTrackingMap is a class level var
showTrackingMap = new LocationCheck((s, ev) =>
{
    if ((ev as LocationCheck.LocationCheckEventArgs).Allowed)
        Console.WriteLine("Present Tracking Map ViewController");
    else
        Console.WriteLine("Disable Tracking Map");
    showTrackingMap.Dispose();
});

LocationCheck encapsulates the location request and the EventHandler will trigger once the user accepts always, only in app or denies the location privacy request.

Note: The EventHandler will also be called if the user is given directions to go to Setting to change the app from previously denied to allowed (always/app in-use).

public class LocationCheck : NSObject, ICLLocationManagerDelegate
{
    public class LocationCheckEventArgs : EventArgs
    {
        public readonly bool Allowed;
        public LocationCheckEventArgs(bool Allowed)
        {
            this.Allowed = Allowed;
        }
    }

    CLLocationManager locationManager;
    EventHandler locationStatus;

    public LocationCheck(EventHandler locationStatus)
    {
        this.locationStatus = locationStatus;
        Initialize();
    }

    public LocationCheck(NSObjectFlag x) : base(x) { Initialize(); }

    public LocationCheck(IntPtr handle) : base(handle) { Initialize(); }

    public LocationCheck(IntPtr handle, bool alloced) : base(handle, alloced) { Initialize(); }

    public void Initialize()
    {
        locationManager = new CLLocationManager
        {
            Delegate = this
        };
        locationManager.RequestAlwaysAuthorization();
    }

    [Export("locationManager:didChangeAuthorizationStatus:")]
    public void AuthorizationChanged(CLLocationManager manager, CLAuthorizationStatus status)
    {
        switch (status)
        {
            case CLAuthorizationStatus.AuthorizedAlways:
            case CLAuthorizationStatus.AuthorizedWhenInUse:
                locationStatus.Invoke(locationManager, new LocationCheckEventArgs(true));
                break;
            case CLAuthorizationStatus.Denied:
            case CLAuthorizationStatus.Restricted:
                locationStatus.Invoke(locationManager, new LocationCheckEventArgs(false));
                break;
        }
    }

    protected override void Dispose(bool disposing)
    {
        locationStatus = null;
        locationManager.Delegate = null;
        locationManager.Dispose();
        base.Dispose(disposing);
    }
}
like image 153
SushiHangover Avatar answered Oct 28 '25 18:10

SushiHangover