Đây là những gì tôi đã làm xét câu trả lời trước: (đây là trong MonoTouch C# nhưng dễ dàng thể dịch để Swift or Obj-C)
Sau đây yêu cầu quyền và sau đó tiến hành cập nhật vị trí nếu được cấp. Nếu không, lần sau người dùng sẽ đến; nếu dịch vụ vị trí bị tắt hoặc bị từ chối, dịch vụ vị trí sẽ hiển thị thông báo để yêu cầu quyền/kích hoạt bằng cách chuyển hướng đến cài đặt
//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);
}
Nguồn
2017-01-30 12:55:47
vẫn không có cách nào nếu người dùng chọn "cho phép" hoặc "từ chối", chỉ một lần thay đổi cho mỗi ứng dụng cài đặt – adali