iOS之访问权限

news/2024/11/17 18:34:17/

从ios7开始,用户可以在设置->隐私->中开启或关闭某些系统权限,比如访问相册,相机 ,通讯录,地图,麦克风等。因此,在我们的程序中,如果要访问系统的某些功能,则最好判断一下权限是否开启。否则用户不能正常使用,也一头雾水,还以为程序出错了。


访问摄像头:

需要导入

#import <AVFoundation/AVFoundation.h>

if(isIOS7AndLater) {NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudioAVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];NSLog(@"---cui--authStatus--------%d",authStatus);// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.if(authStatus ==AVAuthorizationStatusRestricted){NSLog(@"Restricted");}else if(authStatus == AVAuthorizationStatusDenied){// The user has explicitly denied permission for media capture.NSLog(@"Denied");     //应该是这个,如果不允许的话UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"message:@"请在设备的"设置-隐私-相机"中允许访问相机。"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];[alert show];[alert release];return;}else if(authStatus == AVAuthorizationStatusAuthorized){//允许访问// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.NSLog(@"Authorized");}else if(authStatus == AVAuthorizationStatusNotDetermined){// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {if(granted){//点击允许访问时调用//用户明确许可与否,媒体需要捕获,但用户尚未授予或拒绝许可。NSLog(@"Granted access to %@", mediaType);}else {NSLog(@"Not granted access to %@", mediaType);}}];}else {NSLog(@"Unknown authorization status");}
}

麦克风权限检测:

      //检测麦克风功能是否打开[[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) {if (!granted){[ViewUtilalertViewWithString:NSLocalizedString(@"麦克风功能未开启",nil)];}else{[selfrecord:sender];}}];

相册权限检测:需要

#import <AssetsLibrary/AssetsLibrary.h> //导入此类和AssetsLibrary.framework框架

int author = [ALAssetsLibrary authorizationStatus];NSLog(@"author type:%d",author);if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {// The user has explicitly denied permission for media capture.UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"无法使用相册"message:@"请在iPhone的\"设置-隐私-照片\"中允许访问照片。"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];[alert show];[alert release];return;



ALAssetsLibrary详解

ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。

    _library = [[ALAssetsLibrary alloc]init];
    //判断当前应用是否能访问相册资源
    /*
     typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
     ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候
     ALAuthorizationStatusRestricted,        此应用程序没有被授权访问的照片数据。可能是家长控制权限。

     ALAuthorizationStatusDenied,            用户已经明确否认了这一照片数据的应用程序访问.
     ALAuthorizationStatusAuthorized         用户已授权应用访问照片数据.
     }
     */


    int author = [ALAssetsLibrary authorizationStatus];
    NSLog(@"author type:%d",author);


    //关闭监听共享照片流产生的频繁通知信息
    [ALAssetsLibrary disableSharedPhotoStreamsSupport];
    
    //创建一个相册到相册资源中,并通过block返回创建成功的相册ALAssetsGroup
    [_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
        //NSString *const ALAssetsGroupPropertyName;
        //NSString *const ALAssetsGroupPropertyType;
        //NSString *const ALAssetsGroupPropertyPersistentID;
        //NSString *const ALAssetsGroupPropertyURL;
        //查看相册的名字

        NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
        //查看相册的类型
        NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
        //查看相册的存储id
        NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
        //查看相册存储的位置地址
        NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
        groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];


    } failureBlock:^(NSError *error) {
        
    }];


新添加了一个名为test的相册

  [NSThread sleepForTimeInterval:0.5];
    //通过url地址在相册资源中获取该地址的资源文件ALAsset,有可能是相片或视频
    [_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {
        /*
         NSString *const ALAssetPropertyType;
         NSString *const ALAssetPropertyLocation;
         NSString *const ALAssetPropertyDuration;
         NSString *const ALAssetPropertyOrientation;
         NSString *const ALAssetPropertyDate;
         NSString *const ALAssetPropertyRepresentations;
         NSString *const ALAssetPropertyURLs;
         NSString *const ALAssetPropertyAssetURL;
         */

        //查看资源的地理位置信息
        NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
        //如果资源是视频,查看视频的时长
        NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
        //查看资源的方向,图片的旋转方向
        NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
        //查看资源的创建时间
        NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
        //查看资源的描述信息
        NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
        NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
        //查看资源的url路径
        NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
    } failureBlock:^(NSError *error) {
        
    }];
    //通过url地址获取相册资源中的一个相册
    [_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
        NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
    
    }];
    //根据选择的类型遍历相册资源中的相对应类型的所有相册,其中stop行参是指针,表示是否停止迭代,当赋值为false则停止
    /*
     enum {
     ALAssetsGroupLibrary        = (1 << 0),
     ALAssetsGroupAlbum          = (1 << 1),
     ALAssetsGroupEvent          = (1 << 2),
     ALAssetsGroupFaces          = (1 << 3),
     ALAssetsGroupSavedPhotos    = (1 << 4),
     ALAssetsGroupPhotoStream    = (1 << 5),
     ALAssetsGroupAll            = 0xFFFFFFFF,
     };
     */

    [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
        
    }];
    //保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址
    [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    //保存图片到系统默认的相册中,使用cgimageref的形式,并返回照片的url地址
    [_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    
    //保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址
    /*
     typedef enum {
     ALAssetOrientationUp,            // default orientation
     ALAssetOrientationDown,          // 180 deg rotation
     ALAssetOrientationLeft,          // 90 deg CCW
     ALAssetOrientationRight,         // 90 deg CW
     ALAssetOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
     ALAssetOrientationDownMirrored,  // horizontal flip
     ALAssetOrientationLeftMirrored,  // vertical flip
     ALAssetOrientationRightMirrored, // vertical flip
     } ALAssetOrientation;
     */

    UIImage* image = [UIImage imageNamed:@"test.png"];
    [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
        NSLog(@"save image:%@",assetURL);
    }];




http://www.ppmy.cn/news/749991.html

相关文章

iOS 系统权限配置

<------http权限----> <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/><------相册----> <key>Privacy - NSPhotoLibraryUsageDescription</key> <string>App需要…

ios 注意权限设置 描述

NSCameraUsageDescription App需要您的同意&#xff0c;才能访问相机 NSPhotoLibraryUsageDescription App需要您的同意&#xff0c;才能访问相册 NSMicrophoneUsageDescription App需要您的同意&#xff0c;才能访问麦克风 NSContactsUsageDescription App需要您的同意&…

iOS 权限管理

转自&#xff1a;https://www.jianshu.com/p/2d3ef8b72986 网络权限管理 一、iOS网络情况分类&#xff1a; 通过App应用设置网络使用权限&#xff08;关闭、WLAN、WLAN与蜂窝移动网&#xff09;直接设置手机网络情况&#xff08;飞行模式、无线局域网络、蜂窝移动网络&…

手机app权限设置(app系统设置权限)

1. app系统设置权限 手机有没有root呢 有的手机不root也会自带应用权限管理的 但是一般情况下没有root是没有应用权限管理的 如果你root了可以在手机自带软件或安全类软件里面寻找 如果没有你可以先获取权限很简单 你在你的电脑里安一个PC版的应用宝打开手机的设置&#…

ios隐私权限的使用及设置

iOS隐私权限类别如下&#xff1a; 麦克风权限&#xff1a;Privacy - Microphone Usage Description 是否允许此App使用你的麦克风&#xff1f; 相机权限&#xff1a; Privacy - Camera Usage Description 是否允许此App使用你的相机&#xff1f; 相册权限&#xff1a; Privacy…

ios 设置权限

<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能访问媒体资料库</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>App需要您的同意,才能访问蓝牙</string> <key>NSCale…

iOS 网络权限管理

网络权限管理 一、iOS网络情况分类&#xff1a; 通过App应用设置网络使用权限&#xff08;关闭、WLAN、WLAN与蜂窝移动网&#xff09;直接设置手机网络情况&#xff08;飞行模式、无线局域网络、蜂窝移动网络&#xff09; 二、iOS开发使用到的网络判断类&#xff1a; AFNetw…

iOS 全部访问权限设置

plist文件里面添加&#xff0c;Privacy - Photo Library Usage Description&#xff0c;Value值为描述&#xff0c;弹出的提示框会显示出来。 升到iOS10之后&#xff0c;需要设置权限的有&#xff1a; 麦克风权限&#xff1a;Privacy - Microphone Usage Description 是否允许…