Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheesebaron/660441773b7b0b3ec397 to your computer and use it in GitHub Desktop.
Save Cheesebaron/660441773b7b0b3ec397 to your computer and use it in GitHub Desktop.
public static void TryRequestWhenInUseAuthorization(this CLLocationManager locationManager)
{
if (locationManager.RespondsToSelector(new Selector("requestWhenInUseAuthorization")))
locationManager.RequestWhenInUseAuthorization();
}
public static void TryRequestAlwaysAuthorization(this CLLocationManager locationManager)
{
if (locationManager.RespondsToSelector(new Selector("requestAlwaysAuthorization")))
locationManager.RequestAlwaysAuthorization();
}
<key>NSLocationWhenInUseUsageDescription</key>
<string>Message to the user</string>
or
<key>NSLocationAlwaysUsageDescription</key>
<string>Message to the user</string>
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
_locationManager = new CLLocationManager();
_locationManager.AuthorizationChanged += LocationManagerOnAuthorizationChanged;
_locationManager.TryRequestWhenInUseAuthorization();
}
else
{
_mapView.ShowsUserLocation = ViewModel.LocationPermission;
}
var locationManager = new CLLocationManager();
locationManager.AuthorizationChanged += LocationManagerOnAuthorizationChanged;
locationManager.TryRequestWhenInUseAuthorization();
// create your map etc.
private void LocationManagerOnAuthorizationChanged(object sender, CLAuthorizationChangedEventArgs args)
{
if (args.Status == CLAuthorizationStatus.AuthorizedWhenInUse ||
args.Status == CLAuthorizationStatus.AuthorizedAlways)
_mapView.ShowsUserLocation = true;
else
_mapView.ShowsUserLocation = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment