1
您可以查看下面的代碼,它嘗試使用代理嘗試locationManager,並嘗試返回設備的當前位置。Objective C locationManager無法獲取當前位置,總是返回0
但總是返回0(允許位置的權限)
.M
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedAlways) {
// TODO: Handle status authorized always
} else if (status == kCLAuthorizationStatusNotDetermined) {
// TODO: Handle status not determined
} else {
// TODO: Handle other
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//NSLog(@"newLocation: %@", newLocation);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//NSLog(@"Error: %@",[error description]);
}
的info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>We would like to use your location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We would like to use your location.</string>
您是否有很強的參考您的locationManager? – Groot
是@property(強,非原子)CLLocationManager * locationManager; 在.h上聲明 –
你是指什麼意思返回0? – tounaobun