2016-09-19 14:32:55 u011496891 阅读数 4052

Xcode8,iOS10.0系统中,使用相机功能的时候报错

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

主要原因是iOS10之后,相册,位置,麦克风等这些需要权限验证的东西,都需要在plist文件里面添加响应的键值对。否则都会报错,使用Privacy开头的都需要添加到plist文件里面。
这里写图片描述
后面的字符串是授权时候回显示的字样
这里写图片描述

2016-10-28 18:17:06 u013068862 阅读数 5239

1.Xcode8,iOS10的权限设置(不设置会崩溃):

找到项目的info.plist文件,右键open As,以Source Code的形式打开,将以下代码添加进去:

相机权限设置:

<key>NSCameraUsageDescription</key>
    <string>cameraDesciption</string>

相册权限设置:

<key>NSPhotoLibraryUsageDescription</key>
    <string>photoLibraryDesciption</string>

设置好之后,clean一下。


2.在代码中设置代理

@interface MineCtrl ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong)UIImagePickerController *imagePicker;
@property(nonatomic,strong)UIImageView *headShot;

3.设置相关属性

    _imagePicker = [[UIImagePickerController alloc]init];
    _imagePicker.delegate = self;
    _headShot = [[UIImageView alloc]init];
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"];
    _headShot.frame = CGRectMake((SCREEN_WIDTH-80)/2, (SCREEN_HEIGHT/4-80)/2, 80, 80);
<pre name="code" class="objc">   //不设置contentMode,图片会被压扁
    _headShot.contentMode = UIViewContentModeScaleAspectFill;
    [_headShot setClipsToBounds:YES];
    //将选择的图片显示出来
    _headShot.image = [UIImage imageWithContentsOfFile:filePath];


    _headShot.backgroundColor = [UIColor grayColor];
    _headShot.layer.masksToBounds = YES;
    _headShot.layer.cornerRadius = 40;
    _headShot.layer.borderColor = [[UIColor whiteColor]CGColor];

//添加点击手势

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ClickHandle)];
    [tapGesture setNumberOfTapsRequired:1];
    [_headShot addGestureRecognizer:tapGesture];

4.设置相关操作:

#pragma mark - 获取头像
-(void)ClickHandle
{
    UIAlertController *AlertSelect = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:_imagePicker animated:YES completion:nil];
    }];
    UIAlertAction *photo = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:_imagePicker animated:YES completion:nil];
    }];
    UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [AlertSelect addAction:camera];
    [AlertSelect addAction:photo];
    [AlertSelect addAction:cancelAction];
    
    [self presentViewController:AlertSelect animated:YES completion:nil];
    
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
    
    _headShot.image=image;
<pre name="code" class="objc">//将照片存到媒体库
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    [self saveImage:image];
[self dismissViewControllerAnimated:YES completion:nil]; }


4.存储图片到沙盒

#pragma mark - 照片存到本地后的回调
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
    if (!error) {
        NSLog(@"存储成功");
    } else {
        NSLog(@"存储失败:%@", error);
    }
}
#pragma mark - 保存图片
- (void) saveImage:(UIImage *)currentImage {
    //设置照片的品质
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
    NSLog(@"%@",NSHomeDirectory());
    // 获取沙盒目录
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"];
    // 将图片写入文件
    [imageData writeToFile:filePath atomically:NO];    
}
6.最后的样式,即使退出程序,图片也不会消失。





2016-11-16 14:58:38 a910577347 阅读数 3179

  • APP开发避免不开系统权限的问题,如何在APP以更加友好的方式向用户展示系统权限,似乎也是开发过程中值得深思的一件事;

  • 那如何提高APP获取iOS系统权限的通过率呢?有以下几种方式:1.在用户打开APP时就向用户请求权限;2.告知用户授权权限后能够获得好处之后,再向用户请求权限;3.在绝对必要的情况下才向用户请求权限,例如:用户访问照片库时请求访问系统相册权限;4.在展示系统权限的对话框前,先向用户显示自定义的对话框,若用户选择不允许,默认无操作,若用户选择允许,再展示系统对话框。

  • 上述情况在开发过程中是经常遇到的,不同方式的选择会影响最后用户交互体验。这一点感悟正是源于上一周工作遇到的问题:适配iOS10,如何获取应用联网权限用以管理系统对话框的显示管理。当我把这个问题解决后,感觉有必要将常用的iOS系统权限做一个总结,以便后用。



权限分类



  • 联网权限

  • 相册权限

  • 相机、麦克风权限

  • 定位权限

  • 推送权限

  • 通讯录权限

  • 日历、备忘录权限



联网权限



  • 引入头文件 @import CoreTelephony;

  • 应用启动后,检测应用中是否有联网权限


CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){
  //获取联网状态
  switch (state) {
        case kCTCellularDataRestricted:
                  NSLog(@"Restricrted");          
                  break;
        case kCTCellularDataNotRestricted:
                  NSLog(@"Not Restricted");          
                  break;
        case kCTCellularDataRestrictedStateUnknown:
                  NSLog(@"Unknown");          
                  break;
        default:
            break;
  };
};
  • 查询应用是否有联网功能

CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
 switch (state) {
   case kCTCellularDataRestricted:
         NSLog(@"Restricrted");      
         break;
   case kCTCellularDataNotRestricted:
         NSLog(@"Not Restricted");      
         break;  
   case kCTCellularDataRestrictedStateUnknown:
         NSLog(@"Unknown");      
         break;
   default:
         break;
}

相册权限--iOS 9.0之前



  • 导入头文件@import AssetsLibrary;

  • 检查是否有相册权限


PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
  case PHAuthorizationStatusAuthorized:
        NSLog(@"Authorized");      
        break;
  case PHAuthorizationStatusDenied:
        NSLog(@"Denied");      
        break;
  case PHAuthorizationStatusNotDetermined:
        NSLog(@"not Determined");      
        break;  
  case PHAuthorizationStatusRestricted:
        NSLog(@"Restricted");      
        break;  
  default:      
        break;
}
  • ![Uploading 144446-b8aca7ba38c5f8c0_695906.png . . .]获取相册权限

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  if (status == PHAuthorizationStatusAuthorized) {
        NSLog(@"Authorized");
  }else{
        NSLog(@"Denied or Restricted");
  }
  }];

相机和麦克风权限



  • 导入头文件@import AVFoundation;

  • 检查是否有相机或麦克风权限


AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
switch (AVstatus) {
  case AVAuthorizationStatusAuthorized:
        NSLog(@"Authorized");      
        break;  
  case AVAuthorizationStatusDenied:
        NSLog(@"Denied");      
        break;  
  case AVAuthorizationStatusNotDetermined:
        NSLog(@"not Determined");      
        break; 
  case AVAuthorizationStatusRestricted:
        NSLog(@"Restricted");      
        break;  
  default:      
        break;
}
  • 获取相机或麦克风权限

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麦克风权限
  if (granted) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
}];

定位权限



  • 导入头文件@import CoreLocation;

  • 由于iOS8.0之后定位方法的改变,需要在info.plist中进行配置;



  • 检查是否有定位权限

BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (!isLocation) {
  NSLog(@"not turn on the location");}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch (CLstatus) {
  case kCLAuthorizationStatusAuthorizedAlways:
        NSLog(@"Always Authorized");      
        break;
  case kCLAuthorizationStatusAuthorizedWhenInUse:
        NSLog(@"AuthorizedWhenInUse");      
        break;
  case kCLAuthorizationStatusDenied:
        NSLog(@"Denied");      
        break;  
  case kCLAuthorizationStatusNotDetermined:
        NSLog(@"not Determined");      
        break;  
  case kCLAuthorizationStatusRestricted:
        NSLog(@"Restricted");      
        break;  
  default:      
        break;
}
  • 获取定位权限

CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息
  • 在代理方法中查看权限是否改变

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) {
  case kCLAuthorizationStatusAuthorizedAlways:
        NSLog(@"Always Authorized");      
        break;  
  case kCLAuthorizationStatusAuthorizedWhenInUse:
        NSLog(@"AuthorizedWhenInUse");      
        break;  
  case kCLAuthorizationStatusDenied:      
        NSLog(@"Denied");      
        break;  
  case kCLAuthorizationStatusNotDetermined:
        NSLog(@"not Determined");      
        break;  
  case kCLAuthorizationStatusRestricted:
        NSLog(@"Restricted");      
        break;  
  default:
        break;
  }

}

推送权限



  • 检查是否有通讯权限

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];switch (settings.types) {
  case UIUserNotificationTypeNone:
        NSLog(@"None");      
        break;  
  case UIUserNotificationTypeAlert:
        NSLog(@"Alert Notification");      
        break; 
  case UIUserNotificationTypeBadge:
        NSLog(@"Badge Notification");      
        break;  
  case UIUserNotificationTypeSound:
        NSLog(@"sound Notification'");      
        break;  
  default:      
        break;
}
  • 获取推送权限

UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

通讯录权限


  • 导入头文件 @import AddressBook;

  • 检查是否有通讯录权限

 ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();switch (ABstatus) {
   case kABAuthorizationStatusAuthorized:      
         NSLog(@"Authorized");      
         break;  
   case kABAuthorizationStatusDenied:
         NSLog(@"Denied'");      
         break;  
   case kABAuthorizationStatusNotDetermined:
         NSLog(@"not Determined");      
         break;  
   case kABAuthorizationStatusRestricted:
         NSLog(@"Restricted");      
         break;  
   default:      
         break;
}
  • 获取通讯录权限

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
  if (granted) {
        NSLog(@"Authorized");
        CFRelease(addressBook);
  }else{
        NSLog(@"Denied or Restricted");
  }
});

日历、备忘录权限


  • 导入头文件

  • 检查是否有日历或者备忘录权限

typedef NS_ENUM(NSUInteger, EKEntityType) {
  EKEntityTypeEvent,//日历
  EKEntityTypeReminder //备忘
 };

EKAuthorizationStatus EKstatus = [EKEventStore  authorizationStatusForEntityType:EKEntityTypeEvent];switch (EKstatus) {
  case EKAuthorizationStatusAuthorized:
        NSLog(@"Authorized");      
        break;  
  case EKAuthorizationStatusDenied:
        NSLog(@"Denied'");      
        break;  
  case EKAuthorizationStatusNotDetermined:
        NSLog(@"not Determined");      
        break;  
  case EKAuthorizationStatusRestricted:
        NSLog(@"Restricted");      
        break; 
  default:
        break;
}
  • 获取日历或备忘录权限

EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
  if (granted) {
        NSLog(@"Authorized");
  }else{
        NSLog(@"Denied or Restricted");
  }
}];

最后一点


  • 素有获取权限的方法,多用于用户第一次操作应用,iOS 8.0之后,将这些设置都整合在一起,并且可以开启或关闭相应的权限。所有的权限都可以通过下面的方法打开:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
2014-04-04 12:35:15 u013263917 阅读数 24150

  一般当应用在第一次访问相机的时候 系统都会弹出一个提示框,提示用户是否允许访问相机。

 可是假如我们拒绝访问相机,或者在隐私-相机设置里面关闭了应用权限时,再次进入就会直接黑茫茫一片,不会再出现提示框。

那么,如何判断用户的相机权限状况呢?


下面是一句向系统请求访问相机的代码

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {

                NSLog(@"111");

            }];


一般这个是系统默认的,所以不需要我们手动添加,只是稍微了解一下。


进入正题,首先获取相机访问权限状况

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];


然后判断用户的权限

if(authStatus == AVAuthorizationStatusAuthorized)

        {

            NSLog(@"允许状态");

        }

        else if (authStatus == AVAuthorizationStatusDenied)

        {

            NSLog(@"不允许状态,可以弹出一个alertview提示用户在隐私设置中开启权限");

        }

        else if (authStatus == AVAuthorizationStatusNotDetermined)

        {

            NSLog(@"系统还未知是否访问,第一次开启相机时");

        }


2017-03-10 17:52:22 skylin19840101 阅读数 484

升级 iOS 10 之后坑还是挺多的,增加了很多的权限设置,如果不设置,程序则直接crash。

相机

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.



麦克风

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.


地理位置


解决办法

在Info.plist中添加对应的权限即可


iOS权限设置

阅读数 1396

没有更多推荐了,返回首页