如何在某個程序里面判定當前應用程序的定位服務是否可用,其實沒有什么簡單的方法。
這個[CLLocationManager locationServicesEnabled]檢測的是整個iOS系統的位置服務開關,無法檢測當前應用是否被關閉,只能通過CLLocationManagerDelegate的locationManager:didFailWithError:方法去檢測:
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error { NSString *errorString; [manager stopUpdatingLocation]; NSLog(@"Error: %@",[error localizedDescription]); switch([error code]) { case kCLErrorDenied: //Access denied by user errorString = @"Access to Location Services denied by user"; //Do something... break; case kCLErrorLocationUnknown: //Probably temporary... errorString = @"Location data unavailable"; //Do something else... break; default: errorString = @"An unknown error has occurred"; break; } } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; [alert release]; }
參考:http://blog.csdn.net/jinglijun/article/details/8893062
另外還有一種變通的方法:不需要通過上述方式來判斷用戶是否為應用程序開啟了定位服務,而是通過判斷獲取的坐標值是否為空或者為零來判斷用戶是否為應用程序開啟了定位功能。