整個iOS系統(tǒng)的定位服務是否開啟
#import <CoreLocation/CoreLocation.h>
[CLLocationManager locationServicesEnabled];
當前應用定位服務授權
- iOS8以前
@interface RootViewController () <CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager * manager;
@end
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
[_manager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSString *errorString;
NSLog(@"定位失敗原因: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorLocationUnknown:
// do something...
break;
case kCLErrorDenied:
// do something...
break;
......
}
}
定位的錯誤信息
typedef NS_ENUM(NSInteger, CLError) {
// 目前位置是未知的,但CL將繼續(xù)努力
kCLErrorLocationUnknown = 0, // location is currently unknown, but CL will keep trying
// 獲取用戶位置或范圍被拒絕
kCLErrorDenied, // Access to location or ranging has been denied by the user
// 一般情況下,網(wǎng)絡相關的錯誤
kCLErrorNetwork, // general, network-related error
// 標題不能確定
kCLErrorHeadingFailure, // heading could not be determined
// 位置區(qū)域監(jiān)測被用戶拒絕
kCLErrorRegionMonitoringDenied, // Location region monitoring has been denied by the user
// 注冊區(qū)域不能監(jiān)控
kCLErrorRegionMonitoringFailure, // A registered region cannot be monitored
// CL不能立即初始化區(qū)域監(jiān)控
kCLErrorRegionMonitoringSetupDelayed, // CL could not immediately initialize region monitoring
// 如果這個防護事件被提交,提交將不會出現(xiàn)
kCLErrorRegionMonitoringResponseDelayed, // While events for this fence will be delivered, delivery will not occur immediately
// 地理編碼沒有結果
kCLErrorGeocodeFoundNoResult, // A geocode request yielded no result
// 地理編碼產(chǎn)生一部分結果
kCLErrorGeocodeFoundPartialResult, // A geocode request yielded a partial result
// 地理編碼被取消
kCLErrorGeocodeCanceled, // A geocode request was cancelled
// 延遲模式失敗
kCLErrorDeferredFailed, // Deferred mode failed
// 延遲模式失敗了,因為位置更新禁用或暫停
kCLErrorDeferredNotUpdatingLocation, // Deferred mode failed because location updates disabled or paused
// 延遲模式不支持當前精準度
kCLErrorDeferredAccuracyTooLow, // Deferred mode not supported for the requested accuracy
// 延遲模式不支持距離過濾器
kCLErrorDeferredDistanceFiltered, // Deferred mode does not support distance filters
// 延遲模式請求取消前一個請求
kCLErrorDeferredCanceled, // Deferred mode request canceled a previous request
// 測距桿不能執(zhí)行
kCLErrorRangingUnavailable, // Ranging cannot be performed
// 測距失敗
kCLErrorRangingFailure, // General ranging failure
};
-
iOS8以后
iOS8以后位置服務權限需要應用主動向系統(tǒng)注冊授權
1杜耙,在Info.plist添加配置 (可根據(jù)情況任選其一,也可以兩者都添加)
2蚌卤,向系統(tǒng)注冊權限(可根據(jù)情況任選其一,也可以兩者都添加奥秆,與Info.plist中添加的配置對應)
[_manager requestWhenInUseAuthorization];
[_manager requestAlwaysAuthorization];
3逊彭,當應用啟動時,系統(tǒng)會根據(jù)應用的注冊授權彈出提示框請求用戶授權
(彈框的描述信息與Info.plist中配置的描述信息是一直的)
4构订,當我們點擊允許的時候侮叮,定位服務開始生效。
此時持續(xù)調用代理函數(shù)
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"定位中....");
}
當點擊不允許的使用悼瘾,分別調用代理函數(shù)
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
NSLog(@"授權狀態(tài)改變");
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSString *errorString;
NSLog(@"定位失敗原因: %@",[error localizedDescription]);
}
5囊榜,我們再來看看手機設置中的界面
版權聲明:出自MajorLMJ技術博客的原創(chuàng)作品 ,轉載時必須注明出處及相應鏈接亥宿!