回答
這是正確的。
這背後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. Android運行時位置服務權限
- 6. iPhone檢查位置服務
- 7. 支持iOS的11位置的權限,而iOS上的10
- 8. 檢查Facebook上的權限
- 9. angularjs檢查服務器上的授權
- 10. 請求位置更新權限的iOS
- 11. iOS Facebook的SDK - 檢查權限
- 12. 不斷檢查在ios中啓用的位置服務
- 13. 檢查是否已詢問位置服務授權
- 14. 檢查最近的服務器位置
- 15. iOS中的位置服務
- 16. iOS 8不請求位置權限
- 17. 本地化iOS位置權限提示
- 18. iOS MapKit不要求位置權限
- 19. 檢查軌道上的權限查看
- 20. 檢查權限
- 21. 檢查權限?
- 22. iOS:給服務器twitter訪問權限?
- 23. UDP上的TFTP:檢查服務器/客戶端目錄上的寫入權限
- 24. 位置服務?檢測您的位置
- 25. 檢查用戶權限的權限
- 26. WeeApp iOS通知中心小部件:位置服務不保存權限(越獄)
- 27. 如何在授予位置權限後執行任務ios
- 28. 服務的Android運行時位置權限
- 29. 保存的照片相冊名稱無位置服務權限
- 30. MapKit,授予位置服務權限的運行方法
謝謝,這是我需要的。 – aziz 2013-07-14 21:31:35
我認爲這是問題的正確答案。 – 2013-10-24 16:23:42
Wonderfull Stuff – Xeieshan 2014-01-26 20:48:16