LoginSignup
30
29

More than 5 years have passed since last update.

iOS8で現在地が取れない??場合

Posted at

Xcode6にして、いざiPhone6でデバッグだ!

と思ったら、位置情報を取得するところでいつになっても取得できず。
iOS8 SDKからか、位置情報取得の前に、位置情報を使うよって言うのをリクエストするダイアログを自分で出す必要があるらしい。
あと、権限に、今までの「いつでも位置情報使える」「使えない」に加えて「起動中のみ使える」が増えたそうな。

ということで、対応方法は以下のブログより。

I'm Sei.さん、ありがとうございます。
http://im-sei.tumblr.com/post/91824653043/ios-8

if locationManager.respondsToSelector("requestWhenInUseAuthorization") {
    locationManager.requestWhenInUseAuthorization()
}

startUpdateLocationとかする前に呼んでね。

あ、あと、info.plistに以下のキーで、位置情報をなにに使うかの説明文を追加する必要があります。
NSLocationWhenInUseUsageDescription

これでも、実際使用する時は権限取得できたあとにstartUpdatingLocationを呼んだりする必要があるから、結構大変だよね。

サンプル

-(void)start
{
    // 位置情報サービスが使えないとき
    if ([CLLocationManager locationServicesEnabled] == NO) {
        [self locationManager:nil didFailWithError:[NSError errorWithDomain:@"LocationServices disabled" code:1 userInfo:nil]];
        return;
    }

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusNotDetermined:
                [self.locationManager requestWhenInUseAuthorization];
                break;
            case kCLAuthorizationStatusAuthorizedAlways:
            case kCLAuthorizationStatusAuthorizedWhenInUse:
                [self.locationManager startUpdatingLocation];
                break;
            case kCLAuthorizationStatusDenied:
            case kCLAuthorizationStatusRestricted:
                [self locationManager:self.locationManager didFailWithError:[NSError errorWithDomain:@"Location Authorization Denied" code:1 userInfo:nil]];
                break;
        }
    }
    // iOS7未満
    else {
        [self.locationManager startUpdatingLocation];
    }
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            [self.locationManager startUpdatingLocation];
            break;
        case kCLAuthorizationStatusDenied:
        case kCLAuthorizationStatusRestricted:
                [self locationManager:self.locationManager didFailWithError:[NSError errorWithDomain:@"Location Authorization Denied" code:1 userInfo:nil]];
            break;
    }
}

こんな感じ?

30
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
29