IOS 위치정보 권한 설정 요청

위치정보 권한 설정창 요청하는 소스입니다.

CLLocationManager locationManager = [[CLLocationManager alloc] init];

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
switch (status) {
    case kCLAuthorizationStatusNotDetermined: {
        NSLog(@"kCLAuthorizationStatusNotDetermined!");
        [locationManager requestAlwaysAuthorization]; // this will access location automatically if user granted access manually. and will not show apple's request alert twice. (Tested)
    } break;
    case kCLAuthorizationStatusDenied: {
        NSLog(@"User denied location access request!!");
        // show text on label
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled!"
                                                            message:@"Please enable Location Based Services for better results! We promise to keep your location private"
                                                           delegate:self
                                                  cancelButtonTitle:@"Settings"
                                                  otherButtonTitles:@"Cancel", nil];

        //TODO if user has not given permission to device
        if (![CLLocationManager locationServicesEnabled])
        {
            alertView.tag = 100;
        }
        //TODO if user has not given permission to particular app
        else
        {
            alertView.tag = 200;
        }

        NSLog(@"kCLAuthorizationStatusDenied");

        [alertView show];
        [locationManager stopUpdatingLocation];

    } break;
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    {
        NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
    }
    case kCLAuthorizationStatusAuthorizedAlways: {
        // clear text
        NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
        [locationManager startUpdatingLocation]; //Will update location immediately
    } break;
    default:
        break;
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다