Getting user's current location iOS 8.0

夙愿已清 提交于 2019-12-02 07:31:48

Your flow for requesting and starting location update is not proper. First of all the requestAlwaysAuthorization will not change the authorization status immediately because it needs user to press Allow or Don't Allow button in the alert. So you should start location update only after getting its result. Then in didUpdateLocations no need to request/start again.

Do it in following way,

Dn your viewDidLoad

 BOOL isAuthorized = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways;
 if (isAuthorized) {
     [locationManager startUpdatingLocation];
 }
 else {
     [locationManager requestAlwaysAuthorization];
 }

And implement the delegate methods like

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
   CLLocation *current = locations.firstObject;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
        self.serviceLocation.showsUserLocation = YES;
       [locationManager startUpdatingLocation];
    }
}

In iOS 8 for requesting permission you should add the usage description string in the info.plist. In your case the key is NSLocationAlwaysUsageDescription.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!