案例:当前位置,用户在应用安装时选择了“不允许”,那么我是否有办法再次请求用户位置并触发iPhone本地警报以获取当前位置?我在stackoverflow上看到了一些帖子,但它们都比较旧,现在是否有新的SDK调用解决方案或者有人找到了方法?
参考帖子: CLLocation ask again for permission
参考帖子: CLLocation ask again for permission
很抱歉您无法这样做。您可以提示用户更改位置设置。
if (![CLLocationManager locationServicesEnabled])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
switch CLLocationManager.authorizationStatus() {
case .AuthorizedWhenInUse, .Restricted, .Denied:
let alertController = UIAlertController(
title: "Background Location Access Disabled",
message: "In order to be notified, please open this app's settings and set location access to 'Always'.",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open Settings", style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
[CLLocationManager locationServicesEnabled]
检查位置服务是否已启用。
如果您想检查用户是否授权您使用他/她的位置,请检查返回一个布尔值,指示设备上是否启用了位置服务。 用户可以通过在“通用”中切换“位置服务”开关来从“设置”应用程序启用或禁用位置服务。 在开始位置更新之前,您应该检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。当用户首次尝试在应用程序中使用与位置相关的信息时,位置服务会提示用户,但不会提示后续尝试。如果用户拒绝使用位置服务,并且您仍然尝试启动位置更新,则位置管理器会向其委托报告错误。
[CLLocationManager authorizationStatus]
。如果您的应用程序的状态是kCLAuthorizationStatusDenied
,这意味着当您的应用程序请求权限时,用户明确拒绝了您的应用程序。您可以使用此功能相应地通知用户。//Location Manager (foreground)
CLLocationManager locMgr = new CLLocationManager();
locMgr.PausesLocationUpdatesAutomatically = false;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
locMgr.RequestWhenInUseAuthorization();
}
if (CLLocationManager.LocationServicesEnabled && CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse)
{
//set the desired accuracy, in meters
locMgr.DesiredAccuracy = 150;
locMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
Console.WriteLine(e.Locations);
};
locMgr.StartUpdatingLocation();
}
else if (!CLLocationManager.LocationServicesEnabled || CLLocationManager.Status == CLAuthorizationStatus.Denied)
{
var alert = UIAlertController.Create(NSBundle.MainBundle.LocalizedString("Location access", "Location access"), NSBundle.MainBundle.LocalizedString("Please check location", "To show your position on the map, you have to enable location services and authorize the app"), UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Cancel, null));
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
alert.AddAction(UIAlertAction.Create(NSBundle.MainBundle.LocalizedString("Go to settings", "Go to settings"), UIAlertActionStyle.Default, delegate
{
var url = new NSUrl(UIApplication.OpenSettingsUrlString);
UIApplication.SharedApplication.OpenUrl(url);
}));
}
PresentViewController(alert, true, null);
}
我认为没有再次请求位置权限的方法。但是如果您确实需要用户位置,则可以显示警报,指示他们从设置中启用它。