8

設定パネルでアプリケーションの位置情報サービスを無効にしています。View Controller の viewDidLoad でテストを実行して、それらが有効になっているかどうかを確認します。

if([CLLocationManager locationServicesEnabled]) {
   //Do something now
}

このテストは、何らかの理由で常にパスします。ロケーション サービスにアクセスしようとすると、ロケーション マネージャの kCLErrorDenied エラーが発生します。何を与える?

間違ったテストを使用していますか?

4

2 に答える 2

25

locationServicesEnabledクラス メソッドは、位置情報サービスのグローバル設定のみをテストします。私の知る限り、アプリが明示的に拒否されているかどうかをテストする方法はありません。ロケーション要求が失敗するのを待ち、CLLocationManagerDelegate メソッドのlocationManager:didFailWithError:を使用して、必要なことを行う必要があります。例えば:

- (void)locationManager: (CLLocationManager *)manager
       didFailWithError: (NSError *)error {

    NSString *errorString;
    [manager stopUpdatingLocation];
    NSLog(@"Error: %@",[error localizedDescription]);
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user
            errorString = @"Access to Location Services denied by user";
            //Do something...
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            errorString = @"Location data unavailable";
            //Do something else...
            break;
        default:
            errorString = @"An unknown error has occurred";
            break;
        }
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

その他のオプションについては、 CLLocationManager クラス リファレンスの CLError 定数に関するドキュメントを参照してください。

于 2010-08-10T16:36:40.800 に答える
20

+authorizationStatusiOS 4.2 では、CLLocationManagerメソッドを使用してロケーション サービスが拒否されているかどうかを判断できるようになりました。

于 2011-02-03T21:04:11.280 に答える