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

Handle new iOS location permission event (issue 1390) #1487

Merged
merged 5 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}