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".
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With