iOS 全部訪問權限設置


https://www.jianshu.com/p/58cf8b21f268

2017.08.29 12:05* 字數 341 閱讀 444評論 0

plist文件里面添加,Privacy - Photo Library Usage Description,Value值為描述,彈出的提示框會顯示出來。

升到iOS10之后,需要設置權限的有:

麥克風權限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風?
相機權限: Privacy - Camera Usage Description 是否允許此App使用你的相機?
相冊權限: Privacy - Photo Library Usage Description 是否允許此App訪問你的媒體資料庫?
通訊錄權限: Privacy - Contacts Usage Description 是否允許此App訪問你的通訊錄?
藍牙權限:Privacy - Bluetooth Peripheral Usage Description 是否許允此App使用藍牙?
語音轉文字權限:Privacy - Speech Recognition Usage Description 是否允許此App使用語音識別?
日歷權限:Privacy - Calendars Usage Description
定位權限:Privacy - Location When In Use Usage Description
定位權限: Privacy - Location Always Usage Description
位置權限:Privacy - Location Usage Description
媒體庫權限:Privacy - Media Library Usage Description
健康分享權限:Privacy - Health Share Usage Description
健康更新權限:Privacy - Health Update Usage Description
運動使用權限:Privacy - Motion Usage Description
音樂權限:Privacy - Music Usage Description
提醒使用權限:Privacy - Reminders Usage Description
Siri使用權限:Privacy - Siri Usage Description
電視供應商使用權限:Privacy - TV Provider Usage Description
視頻用戶賬號使用權限:Privacy - Video Subscriber Account Usage Description

權限的plist


    <key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能訪問媒體資料庫</string> <key>NSBluetoothPeripheralUsageDescription</key> <string>App需要您的同意,才能訪問藍牙</string> <key>NSCalendarsUsageDescription</key> <string>App需要您的同意,才能訪問日歷</string> <key>NSCameraUsageDescription</key> <string>App需要您的同意,才能訪問相機</string> <key>NSHealthShareUsageDescription</key> <string>App需要您的同意,才能訪問健康分享</string> <key>NSHealthUpdateUsageDescription</key> <string>App需要您的同意,才能訪問健康更新 </string> <key>NSLocationAlwaysUsageDescription</key> <string>App需要您的同意,才能始終訪問位置</string> <key>NSLocationUsageDescription</key> <string>App需要您的同意,才能訪問位置</string> <key>NSLocationWhenInUseUsageDescription</key> <string>App需要您的同意,才能在使用期間訪問位置</string> <key>NSMicrophoneUsageDescription</key> <string>App需要您的同意,才能訪問麥克風</string> <key>NSMotionUsageDescription</key> <string>App需要您的同意,才能訪問運動與健身</string> <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能訪問相冊</string> <key>NSRemindersUsageDescription</key> <string>App需要您的同意,才能訪問提醒事項</string>


ios訪問權限

https://blog.csdn.net/LVXIANGAN/article/details/47172337

從ios7開始,用戶可以在設置->隱私->中開啟或關閉某些系統權限,比如訪問相冊,相機 ,通訊錄,地圖,麥克風等。因此,在我們的程序中,如果要訪問系統的某些功能,則最好判斷一下權限是否開啟。否則用戶不能正常使用,也一頭霧水,還以為程序出錯了。

 

訪問攝像頭:

需要導入

 

#import <AVFoundation/AVFoundation.h>

 

  1.  
    if(isIOS7AndLater) {
  2.  
     
  3.  
    NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio
  4.  
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  5.  
    NSLog(@"---cui--authStatus--------%d",authStatus);
  6.  
    // This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
  7.  
    if(authStatus ==AVAuthorizationStatusRestricted){
  8.  
    NSLog(@"Restricted");
  9.  
    } else if(authStatus == AVAuthorizationStatusDenied){
  10.  
    // The user has explicitly denied permission for media capture.
  11.  
    NSLog(@"Denied"); //應該是這個,如果不允許的話
  12.  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
  13.  
    message: @"請在設備的"設置-隱私-相機"中允許訪問相機。"
  14.  
    delegate: self
  15.  
    cancelButtonTitle: @"確定"
  16.  
    otherButtonTitles: nil];
  17.  
    [alert show];
  18.  
    [alert release];
  19.  
    return;
  20.  
    }
  21.  
    else if(authStatus == AVAuthorizationStatusAuthorized){//允許訪問
  22.  
    // The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
  23.  
    NSLog(@"Authorized");
  24.  
     
  25.  
    } else if(authStatus == AVAuthorizationStatusNotDetermined){
  26.  
    // Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
  27.  
    [ AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
  28.  
    if(granted){//點擊允許訪問時調用
  29.  
    //用戶明確許可與否,媒體需要捕獲,但用戶尚未授予或拒絕許可。
  30.  
    NSLog(@"Granted access to %@", mediaType);
  31.  
    }
  32.  
    else {
  33.  
    NSLog(@"Not granted access to %@", mediaType);
  34.  
    }
  35.  
     
  36.  
    }];
  37.  
    } else {
  38.  
    NSLog(@"Unknown authorization status");
  39.  
    }
  40.  
    }

麥克風權限檢測:

 

  1.  
    //檢測麥克風功能是否打開
  2.  
    [[ AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) {
  3.  
    if (!granted)
  4.  
    {
  5.  
    [ViewUtilalertViewWithString: NSLocalizedString(@"麥克風功能未開啟",nil)];
  6.  
    }
  7.  
    else
  8.  
    {
  9.  
     
  10.  
    [selfrecord:sender];
  11.  
    }
  12.  
    }];

相冊權限檢測:需要

 

#import <AssetsLibrary/AssetsLibrary.h> //導入此類和AssetsLibrary.framework框架

 

  1.  
    int author = [ALAssetsLibrary authorizationStatus];
  2.  
    NSLog(@"author type:%d",author);
  3.  
    if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
  4.  
    // The user has explicitly denied permission for media capture.
  5.  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"無法使用相冊"
  6.  
    message: @"請在iPhone的\"設置-隱私-照片\"中允許訪問照片。"
  7.  
    delegate: self
  8.  
    cancelButtonTitle: @"確定"
  9.  
    otherButtonTitles: nil];
  10.  
    [alert show];
  11.  
    [alert release];
  12.  
    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);
    }];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM