![](/img/trans.png)
[英]IOS 8 Map is not working: Trying to start MapKit location updates without prompting for location authorization
[英]Trying to start MapKit location updates without prompting for location authorization in ios 9 with objective-c
我試圖在iOS 8上使用MapKit,但不斷收到錯誤消息:
Trying to start MapKit location updates without prompting for location authorization. Must call
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
在這里查找,我發現我必須在plist中實現NSLocationWhenInUsageDescription,但是什么也沒有發生,並且在控制台中仍然出現該錯誤。 我究竟做錯了什么?
1.-將以下行添加到您的info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
2.-提示位置授權:
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
3.-當用戶接受后更新位置...
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
}
}
只需在CLLocationManager
初始化中添加這些代碼即可。
例如: @property (strong, nonatomic) CLLocationManager *locationManager;
然后在您的實現中只需調用[self.locationManager requestWhenInUseAuthorization];
在調用更新函數之前。
在您的cllocationmanager
初始化之后,檢查ios 8並尋求許可
locationManager = [[CLLocationManager alloc] init];// [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
參考http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
除了在PList中實現鍵(我具有NSLocationWhenInUseUsageDescritpion
和NSLocationWhenInUseUsageDescritpion
Privacy - Location Usage Description
)之外,我還使用以下代碼解決了警告:
if ([CLLocationManager locationServicesEnabled])
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusNotDetermined) {
} else {
_mapView.showsUserLocation = YES;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.