回答
这是正确的。
这背后if ([CLLocationManager locationServicesEnabled]){
NSLog(@"Location Services Enabled");
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
alert = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
检查CLLocationManager的locationServicesEnabled属性来检查系统范围的可用性。使用您的CLLocationManagerDelegate的locationManager:didFailWithError:方法并检查kCLErrorDenied错误以查看用户是否拒绝了位置服务。
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
if (!locationAllowed)
{
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];
[alert release];
}
为您的应用程序使用此代码
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
如果位置服务禁用您的应用程序,然后它给你错误
Error Domain=kCLErrorDomain Code=1 "The operation couldn’t be completed. (kCLErrorDomain error 1.)"
我需要一个示例代码,您能否提供一个关于它的帖子? – 2013-03-01 08:05:11
检查我编辑的答案 – 2013-03-01 08:09:17
我把这段代码放在viewDidLoad中,locationAllowed总是返回YES。 – 2013-03-01 08:19:17
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",error.userInfo);
if([CLLocationManager locationServicesEnabled]){
NSLog(@"Location Services Enabled");
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"App Permission Denied"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
}
原因,这种方法会打电话的时候你的服务将被关闭定位服务。这段代码对我很有用。
经过大量的调查。我建议在标签上显示此消息,而不是在警报视图中显示。因为有很多情况需要测试(用户通常禁用位置服务,或者仅用于应用程序,删除应用程序,重新安装)。
其中一种情况会导致您的警报同时显示您的消息以及苹果的警报消息。你的警报将会落后于苹果的警惕。这是一个令人困惑和不合逻辑的行为。
我提出以下建议:
斯威夫特3:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
Log.verbose("User still thinking granting location access!")
manager.startUpdatingLocation() // this will access location automatically if user granted access manually. and will not show apple's request alert twice. (Tested)
break
case .denied:
Log.verbose("User denied location access request!!")
// show text on label
label.text = "To re-enable, please go to Settings and turn on Location Service for this app."
manager.stopUpdatingLocation()
loadingView.stopLoading()
break
case .authorizedWhenInUse:
// clear text
label.text = ""
manager.startUpdatingLocation() //Will update location immediately
break
case .authorizedAlways:
// clear text
label.text = ""
manager.startUpdatingLocation() //Will update location immediately
break
default:
break
}
}
的Objective-C:
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
DDLogVerbose(@"User still thinking granting location access!");
[locationManager startUpdatingLocation]; // this will access location automatically if user granted access manually. and will not show apple's request alert twice. (Tested)
} break;
case kCLAuthorizationStatusDenied: {
DDLogVerbose(@"User denied location access request!!");
// show text on label
label.text = @"To re-enable, please go to Settings and turn on Location Service for this app.";
[locationManager stopUpdatingLocation];
[loadingView stopLoading];
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
// clear text
label.text = @"";
[locationManager startUpdatingLocation]; //Will update location immediately
} break;
default:
break;
}
}
测试在iOS 9.2
为了得到locatio ñ更新要经常检查用户的iOS设备启用
- 位置服务和
- 位置服务启用特定应用
,正确设置屏幕上推出用户启用
启动iOS设备位置设置页面
步骤。1转到项目设置 - >信息 - > URL类型 - >添加新的URL方案
Step.2使用下面的代码,推出直拨电话的位置设置页面: (注:URL计划是在iOS的不同10+,我们检查版本说明here)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice
currentDevice] systemVersion] compare:v options:NSNumericSearch] ==
NSOrderedAscending)
//Usage
NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
启动应用程序部位S ettings页
下面的代码用于启动直接应用程序的位置设置页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
以下是完整的代码示例: 的#define SYSTEM_VERSION_LESS_THAN(V)([[[的UIDevice currentDevice ] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
CLLocationManager *locationManager;
-(void) checkLocationServicesAndStartUpdates
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
//Checking authorization status
if (![CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled!"
message:@"Please enable Location Based Services for better results! We promise to keep your location private"
delegate:self
cancelButtonTitle:@"Settings"
otherButtonTitles:@"Cancel", nil];
//TODO if user has not given permission to device
if (![CLLocationManager locationServicesEnabled])
{
alertView.tag = 100;
}
//TODO if user has not given permission to particular app
else
{
alertView.tag = 200;
}
[alertView show];
return;
}
else
{
//Location Services Enabled, let's start location updates
[locationManager startUpdatingLocation];
}
}
处理用户点击输入反应,并启动正确的位置设置
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)//Settings button pressed
{
if (alertView.tag == 100)
{
//This will open ios devices location settings
NSString* url = SYSTEM_VERSION_LESS_THAN(@"10.0") ? @"prefs:root=LOCATION_SERVICES" : @"App-Prefs:root=Privacy&path=LOCATION";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
else if (alertView.tag == 200)
{
//This will opne particular app location settings
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
else if(buttonIndex == 1)//Cancel button pressed.
{
//TODO for cancel
}
}
不是&&应该是OR(||)in if![CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)' – 2016-05-16 13:48:03
这应该是疯狂的表决!迄今为止最快最简单的解决方案! – greenhouse 2016-06-22 10:59:02
谢谢@greenhouse我很高兴它可以帮助你:) – swiftBoy 2016-06-22 11:30:38
最好的办法,处理所有的情况下! - >
//First, checking if the location services are enabled
if(![CLLocationManager locationServicesEnabled]){
[self showMessage:@"Please enable location services to detect location!" withTitle:@"Location not enabled"];
}
else if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
//Now if the location is denied.
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Enable location permission"
message:@"To auto detect location, please enable location services for this app"
preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tintColor = AppColor;
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Dismiss"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *goToSettings = [UIAlertAction
actionWithTitle:@"Settings"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
//Simple way to open settings module
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}];
[alertController addAction:cancelAction];
[alertController addAction:goToSettings];
[self presentViewController:alertController animated:YES completion:^{
alertController.view.tintColor = AppColor;
}];
}
else{
//Do whatever you want here
}
Swift 3.0 & iOS 10 Solution:
self.locationManager?.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() != CLAuthorizationStatus.denied {
locationManager?.delegate = self
locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager?.distanceFilter = distanceFiler
locationManager?.startUpdatingLocation()
}else{
let alertView = UIAlertView(title: "Location Services Disabled!", message: "Please enable Location Based Services for better results! We promise to keep your location private", delegate: self, cancelButtonTitle: "Settings", otherButtonTitles: "Cancel")
alertView.delegate = self
alertView.show()
return
}
@objc(alertView:clickedButtonAtIndex:) func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
if buttonIndex == 0 {
if let url = URL(string: "App-Prefs:root=LOCATION_SERVICES") {
UIApplication.shared.open(url, completionHandler: .none)
}
}
else if buttonIndex == 1 {
//TODO for cancel
}
}
- 1. 位置服务权限未保存iOS
- 2. 位置权限检查和授权
- 3. 检查iOS定位服务
- 4. 位置服务权限为空
- 5. Iphone地理位置权限检查
- 6. 支持iOS的11位置的权限,而iOS上的10
- 7. 检查Facebook上的权限
- 8. angularjs检查服务器上的授权
- 9. iOS Facebook的SDK - 检查权限
- 10. 检查是否已询问位置服务授权
- 11. iOS中的位置服务
- 12. 检查最近的服务器位置
- 13. iOS 8不请求位置权限
- 14. 本地化iOS位置权限提示
- 15. iOS:给服务器twitter访问权限?
- 16. 检查权限
- 17. 位置服务?检测您的位置
- 18. WeeApp iOS通知中心小部件:位置服务不保存权限(越狱)
- 19. 如何在授予位置权限后执行任务ios
- 20. 服务的Android运行时位置权限
- 21. 保存的照片相册名称无位置服务权限
- 22. 询问服务内部的位置权限
- 23. 如何提供android M的权限以运行位置服务?
- 24. 如何让位置服务权限的MessageBox
- 25. Android 6.0上的蓝牙位置权限
- 26. 哪里是检查对象权限的最佳位置?
- 27. Web服务器上的文件权限?
- 28. 位置权限对话框会在iOS 10的iOS 10
- 29. 位置权限问题11的iOS和iOS 10
- 30. Windows服务权限
谢谢,这是我需要的。 – aziz 2013-07-14 21:31:35
我认为这是问题的正确答案。 – 2013-10-24 16:23:42
Wonderfull Stuff – Xeieshan 2014-01-26 20:48:16