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

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

 

LPDeviceManger.h

#import <Foundation/Foundation.h>

@interface LPDeviceManager : NSObject

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

@end

LPDeviceManger.m

#import <AVFoundation/AVFoundation.h>
#import "LPDeviceManager.h"
#import <AssetsLibrary/AssetsLibrary.h>

@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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值