相册,相机,通讯录访问权限标准写法

本文介绍了一个Objective-C类,用于检查和提示用户授予应用访问相册、相机和通讯录的权限。当权限被拒绝或受限时,会显示警告框引导用户前往设置页面开启相应的权限。

@implementation OTUniversalMethod (VisitDevice)

- (BOOL)isAvailablyForAlbum {

    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];

    if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){

        NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];

        NSString *msg = [NSString stringWithFormat:@"请在iPhone设置”-“隐私”-“照片选项中,允许%@访问你的照片。",appName];

        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"相册服务未开启" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"立即开启", nil];

        alert.delegate = self;

        alert.tag = HOS_ALERT_TAG_ALBUM;

        [alert show];

        return NO;

    }

    return YES;

}


- (BOOL)isAvailablyForCamera {

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    if(authStatus  == AVAuthorizationStatusRestricted ||

       authStatus == AVAuthorizationStatusDenied ) {

        NSString* msg = [NSString stringWithFormat:@"请在设置”-“隐私”-“相机选项中,允许%@访问你的相机。",[[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleName"]];

        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"相机服务未开启" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"立即开启", nil];

        alterView.delegate = self;

        alterView.tag = HOS_ALERT_TAG_CAMERA;

        [alterView show];

        return NO;

    }

    return YES;

}


- (BOOL)isAvailablyForAddressBook {

    

    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();

    if(authStatus == kABAuthorizationStatusRestricted || authStatus ==

       kABAuthorizationStatusDenied){

        NSString* msg = [NSString stringWithFormat:@"请在设置-隐私-通讯录选项中,允许%@访问你的通讯录。",[[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleName"]];

        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"相机服务未开启" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"立即开启", nil];

        alterView.delegate = self;

        alterView.tag = HOS_ALERT_TAG_ADDRESSBOOK;

        [alterView show];

        return NO;

    }

    return YES;

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (alertView.tag == HOS_ALERT_TAG_ALBUM) {

        if (buttonIndex == 1) {

            if(IOS8_And_Later){

                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

                }

            }

            

        }

    }

    

    if (alertView.tag == HOS_ALERT_TAG_CAMERA) {

        if (buttonIndex == 1) {

            if(IOS8_And_Later){

                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

                }

            }

        }

    }

    

    if (alertView.tag == HOS_ALERT_TAG_ADDRESSBOOK) {

        if (buttonIndex == 1) {

            if(IOS8_And_Later){

                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

                }

            }

        }

    }

}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值