Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Handle new iOS location permission event (issue 1390) (#1487)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
msjogren and mattleibow authored Nov 6, 2020
1 parent 095969b commit c3a3f31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Xamarin.Essentials/Permissions/Permissions.ios.tvos.watchos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ internal static Task<PermissionStatus> RequestLocationAsync(bool whenInUse, Acti
return Task.FromResult(PermissionStatus.Disabled);

locationManager = new CLLocationManager();
var previousState = locationManager.GetAuthorizationStatus();

var tcs = new TaskCompletionSource<PermissionStatus>(locationManager);

var previousState = CLLocationManager.Status;

locationManager.AuthorizationChanged += LocationAuthCallback;
var del = new ManagerDelegate();
del.AuthorizationStatusChanged += LocationAuthCallback;
locationManager.Delegate = del;

invokeRequest(locationManager);

Expand All @@ -165,7 +166,7 @@ void LocationAuthCallback(object sender, CLAuthorizationChangedEventArgs e)
// Wait for a timeout to see if the check is complete
if (tcs != null && !tcs.Task.IsCompleted)
{
locationManager.AuthorizationChanged -= LocationAuthCallback;
del.AuthorizationStatusChanged -= LocationAuthCallback;
tcs.TrySetResult(GetLocationStatus(whenInUse));
}
}
Expand All @@ -184,8 +185,7 @@ void LocationAuthCallback(object sender, CLAuthorizationChangedEventArgs e)
}
}

locationManager.AuthorizationChanged -= LocationAuthCallback;

del.AuthorizationStatusChanged -= LocationAuthCallback;
tcs.TrySetResult(GetLocationStatus(whenInUse));
locationManager?.Dispose();
locationManager = null;
Expand All @@ -199,6 +199,19 @@ void LocationAuthCallback(object sender, CLAuthorizationChangedEventArgs e)
}
}
}

class ManagerDelegate : NSObject, ICLLocationManagerDelegate
{
public event EventHandler<CLAuthorizationChangedEventArgs> AuthorizationStatusChanged;

[Export("locationManager:didChangeAuthorizationStatus:")]
public void AuthorizationChanged(CLLocationManager manager, CLAuthorizationStatus status) =>
AuthorizationStatusChanged?.Invoke(this, new CLAuthorizationChangedEventArgs(status));

[Export("locationManagerDidChangeAuthorization:")]
public void DidChangeAuthorization(CLLocationManager manager) =>
AuthorizationStatusChanged?.Invoke(this, new CLAuthorizationChangedEventArgs(manager.AuthorizationStatus));
}
}

public partial class LocationAlways : BasePlatformPermission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,20 @@ internal static DateTimeOffset ToDateTime(this NSDate timestamp)
return DateTimeOffset.UtcNow;
}
}

internal static CLAuthorizationStatus GetAuthorizationStatus(this CLLocationManager locationManager)
{
#if !__MACOS__ // this is coming in macOS 11
#if __WATCHOS__
if (Platform.HasOSVersion(7, 0))
#else
if (Platform.HasOSVersion(14, 0))
#endif
return locationManager.AuthorizationStatus;

#endif

return CLLocationManager.Status;
}
}
}

0 comments on commit c3a3f31

Please sign in to comment.