获取用户授权,如:定位,通知,录音等。

获取用户位置权限

iOS获取系统相关权限(iOS 7以上)

  • 先来看看位置的一些权限:
kCLAuthorizationStatusRestricted:定位服务授权状态是受限制的。可能是由于活动限制定位服务,用户不能改变。这个状态可能不是用户拒绝的定位服务。
kCLAuthorizationStatusDenied:定位服务授权状态已经被用户明确禁止,或者在设置里的定位服务中关闭。
kCLAuthorizationStatusAuthorizedAlways:定位服务授权状态已经被用户允许在任何状态下获取位置信息。包括监测区域、访问区域、或者在有显著的位置变化的时候。
kCLAuthorizationStatusAuthorizedWhenInUse:定位服务授权状态仅被允许在使用应用程序的时候。
kCLAuthorizationStatusAuthorized:这个枚举值已经被废弃了。他相当于kCLAuthorizationStatusAuthorizedAlways这个值。

这里就列出我用的定位的代码:(比较简单的)
这里你当然需要引入

  • 框架#import
  • 声明属性@property (strong,nonatomic) CLLocationManager* locationManager;
  • 遵循代理
-(void)startLocation{
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 100.0f;
    
    // 最好是用以下的方法写,而不是去用系统版本判断之后请求权限,这样可以避免当你没有打开background模式时出现崩溃的问题。
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [self.locationManager requestWhenInUseAuthorization];
    }
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
 
    if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
        // 直接前往改程序设置定位的地方
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请打开定位权限" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"前往设置", nil];
        alertView.delegate = self;
        [alertView show];
    }
    [self.locationManager startUpdatingLocation];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        UIApplication *app = [UIApplication sharedApplication];
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([app canOpenURL:settingsURL]) {
            [app openURL:settingsURL];
        }
    }
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
                [self.locationManager requestWhenInUseAuthorization];
            }break;
        default:break;
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *newLocation = locations[0];
    CLLocationCoordinate2D oldCoordinate = newLocation.coordinate;
    
    // 下面代码可以防止,这个代理被调用多次。这样处理可以让代理只调用一次
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 1.0) {  // 如果调用已经一次,不再执行
        return;
    }
    NSLog(@"经度:%f,纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
    [manager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *_Nullable placemarks, NSError * _Nullable error) {
        for (CLPlacemark *place in placemarks) {
            NSLog(@"country,%@",place.country);                // 国家
            NSLog(@"locality,%@",place.locality);              // 市
            NSLog(@"subLocality,%@",place.subLocality);        // 区
            NSLog(@"thoroughfare,%@",place.thoroughfare);      // 街道
            NSLog(@"name,%@",place.name);                      // 位置名
            NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道
        }
    }];
}

获取用户是否开启通知

- (BOOL)isAllowedNotification {
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (setting.types != UIUserNotificationTypeNone) {
            return YES;
        }
    } else {
        
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(type != UIRemoteNotificationTypeNone)
            return YES;
    }
    return NO;
}

获取AVAudioSession录音权限

 if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]){
        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted)
         if (granted) {
             //用户已允许,直接进行录制等,逻辑操作...
         }else {
             //用户拒绝,引导用户开启麦克风服务,直接跳转到该应用设置页面
         }
         }];
    }

你可能感兴趣的:(获取用户授权,如:定位,通知,录音等。)