iOS8 이상에서 LocationManager 작동 안함

-- iOS (iPhone) 2016. 5. 23. 23:27
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

예전에 지도서비스를 하는 앱을 만들었었는데 갑자기 아래의 오류를 내면서 지도서비스가 제대로 작동하지 않았다.


Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first

이유는 보안강화를 위해 iOS8 이상부터 몇 가지 추가를 해야 하기 때문이다.
( 요새 안드로이드와 iOS가 점점 닮아간다는 것을 자주 느낀다. )


1. plist 파일에 설정추가

아래의 두 옵션 중 적절한 옵션을 선택하여 plist파일에 추가해준다.

  1) NSLocationWhenInUseUsageDescription : 앱이 활성화 되어 있는 도중에만 위치서비스를 이용하는 경우
  2) NSLocationAlwaysUsageDescription : 앱이 백그라운드에서도 위치서비스를 계속 이용해야 하는 경우


2. LocationManager 객체선언 후 아래의 코드 추가

self.locationManager = [[CLLocationManager alloc] init];

// iOS8이상에서만 적용
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    // 위에서 NSLocationWhenInUseUsageDescription를 지정한 경우
    // [self.locationManager requestWhenInUseAuthorization];

    // 위에서 NSLocationAlwaysUsageDescription를 지정한 경우
    // [self.locationManager requestAlwaysAuthorization];
}


posted by 어린왕자악꿍