【发布时间】:2015-08-10 07:06:17
【问题描述】:
我正在尝试使用实现MKMapView 的实例,使用CoreLocation 跟踪用户位置,然后放大到他们所在的位置。
我只想在我在前台时跟踪用户的位置。由于我的应用程序是针对 iOS8 的,所以我有一个键 NSLocationWhenInUseUsageDescription 的 plist 条目。
当我第一次运行该应用程序时,该应用程序会适当地询问它是否可以访问我的位置。单击“允许”后,我会从 Xcode 收到以下警告:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
...这有点令人困惑,因为我实际上是在调用requestWhenInUseAuthorization,如下面的代码所示:
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;
@end
@implementation MapView
- (void)viewDidLoad {
[super viewDidLoad];
[self locationManager];
[self updateLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
self.locationManager = nil;
}
- (CLLocationManager *)locationManager {
//We only want to get the location when the app is in the foreground
[_locationManager requestWhenInUseAuthorization];
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return _locationManager;
}
- (void)updateLocation {
_mapView.userTrackingMode = YES;
[self.locationManager startUpdatingLocation];
}
有没有人知道为什么会出现这个警告?
【问题讨论】:
标签: ios objective-c cocoa-touch mkmapview core-location