iOS 关于 定位、相机、相册权限处理

苹果AppStore对于私有API的检测越来越严格,下边给大家提供定位、相机、相册的权限处理方法,在这个封装了一个工具类,感兴趣的可以拿走。后续会继续加入其他权限,欢迎经常来逛逛。

 

LPDeviceManger.h

#import 

@interface LPDeviceManager : NSObject

/**
 检测用户是否开启位置权限
 */
+(BOOL)checkUserLocationAuth;
/**
 检测用户是否开启相机权限
 */
+(BOOL)checkUserCameraAuth;
/**
 检测用户是否开启相册权限
 */
+(BOOL)checkUserPhotoAuth;

@end

LPDeviceManger.m

#import 
#import "LPDeviceManager.h"
#import 

@implementation LPDeviceManager

/**
 定位权限

 @return YES  NO
 */
+(BOOL)checkUserLocationAuth {
    CLAuthorizationStatus author = [CLLocationManager authorizationStatus];
    if (author == kCLAuthorizationStatusDenied || author == kCLAuthorizationStatusRestricted) {
        [self showAlertWithMessage:@"为了更好的体验,请到设置->隐私->定位服务中开启!"];
        return NO;
    }
    return YES;
}
/**
 相机权限
 
 @return YES  NO
 */
+(BOOL)checkUserCameraAuth {
    AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (author == AVAuthorizationStatusRestricted || author == AVAuthorizationStatusDenied) {
        [self showAlertWithMessage:@"应用相机权限受限,请在\"设置-隐私-相机\"中启用"];
        return NO;
    }
    return YES;
}
/**
 相册权限
 
 @return YES  NO
 */
+(BOOL)checkUserPhotoAuth {
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author == AVAuthorizationStatusRestricted || author == AVAuthorizationStatusDenied) {
        [self showAlertWithMessage:@"应用相册权限受限,请在\"设置-隐私-相册\"中启用"];
        return NO;
    }
    return YES;
}

+(void)showAlertWithMessage:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"去开启" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:settingURL]) {
            [[UIApplication sharedApplication] openURL:settingURL];
        }
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    UIViewController *root = [UIApplication sharedApplication].delegate.window.rootViewController;
    [root presentViewController:alert animated:YES completion:nil];
}
@end

 

你可能感兴趣的:(Objective-C)