2018-01-10 15:24:56 iosWorker 阅读数 769
  • iOS开发-全面解析iOS蓝牙BLE4.0开发

    只要你会OC基础,会写HelloWorld,你就可以实现iOS的蓝牙通信功能,实现蓝牙小项目也不在话下,作者会带领大家详细分析BLE4.0原理,通过分析xcode使用的蓝牙API,让学者能够得心应手的实现蓝牙BLE4.0的开发

    3668 人正在学习 去看看 许英俊

直接上代码+截图
(http://img.blog.csdn.net/20180110151849217?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaW9zV29ya2Vy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
相机+相册权限 及其描述

iOS11之后:默认开启访问相册权限(读权限),无需用户授权,无需添加NSPhotoLibraryUsageDescription,适配iOS11之前的还是需要加的。 添加图片到相册(写权限),需要用户授权,需要添加 NSPhotoLibraryAddUsageDescription。

NSPhotoLibraryAddUsageDescription 是否允许此APP保存图片到相册?

2017-05-11 16:43:14 sinat_17775997 阅读数 6793
  • iOS开发-全面解析iOS蓝牙BLE4.0开发

    只要你会OC基础,会写HelloWorld,你就可以实现iOS的蓝牙通信功能,实现蓝牙小项目也不在话下,作者会带领大家详细分析BLE4.0原理,通过分析xcode使用的蓝牙API,让学者能够得心应手的实现蓝牙BLE4.0的开发

    3668 人正在学习 去看看 许英俊

iOS开发中如果要调用相机拍取照片或者是直接获取相册中的照片,那么调用UIImagePickerController是个不错的选择。UIImagePickerController继承于UINavigationController,使用代理方法时需要同时遵守这两个控制器的协议,它不仅可以用来选取图片,其实它的还能用来拍摄视频。http://blog.csdn.net/magical_code/article/details/51132664

1.UIImagePickerController简介

UIImagePickerController是系统提供的用来获取图片或视频的接口,使用UIImagePickerController类来获取图片的基本步骤如下:

  • 初始化UIImagePickerController类
  • 设置UIImagePickerController实例的数据来源
  • 设置UIImagePickerController实例的代理
  • 设置是否允许编辑图片,若允许则allowsEditing属性值置为YES
  •  设置完UIImagePickerController实例的属性之后,在需要获取图片时要跳转到图像选取控制器当中去选取或拍摄图片
  • 完成图片的选取后回调代理方法

UIImagePickerController实例的三种数据来源:

  1. typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
  2. UIImagePickerControllerSourceTypePhotoLibrary, // 来自图库
  3. UIImagePickerControllerSourceTypeCamera, // 来自相机
  4. UIImagePickerControllerSourceTypeSavedPhotosAlbum // 来自相册
  5. };

2.实际测试相机和图库

本次构建的应用允许用户通过相机或者图库来选取一张图像并在imageView当中显示。

2-1.设计界面

在故事板中为程序的主界面添加一个imageView和一个button,并为他们添加两个输出接口:

QQ20160330-0

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  4.  
  5. @property (weak, nonatomic) IBOutlet UIImageView *imageView;
  6. - (IBAction)chooseImage:(id)sender;
  7.  
  8. @end

2-2.功能实现

在ViewController.m中实现按钮的点击方法:

  1. - (IBAction)chooseImage:(id)sender {
  2. // 创建UIImagePickerController实例
  3. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  4. // 设置代理
  5. imagePickerController.delegate = self;
  6. // 是否允许编辑(默认为NO)
  7. imagePickerController.allowsEditing = YES;
  8. // 创建一个警告控制器
  9. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  10. // 设置警告响应事件
  11. UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  12. // 设置照片来源为相机
  13. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  14. // 设置进入相机时使用前置或后置摄像头
  15. imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
  16. // 展示选取照片控制器
  17. [self presentViewController:imagePickerController animated:YES completion:^{}];
  18. }];
  19. UIAlertAction *photosAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  20. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  21. [self presentViewController:imagePickerController animated:YES completion:^{}];
  22. }];
  23. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  24. }];
  25. // 判断是否支持相机
  26. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  27. {
  28. // 添加警告按钮
  29. [alert addAction:cameraAction];
  30. }
  31. [alert addAction:photosAction];
  32. [alert addAction:cancelAction];
  33. // 展示警告控制器
  34. [self presentViewController:alert animated:YES completion:nil];
  35. }

设置UIImagePickerController的代理方法:

  1. #pragma mark - UIImagePickerControllerDelegate
  2. // 完成图片的选取后调用的方法
  3. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  4. // 选取完图片后跳转回原控制器
  5. [picker dismissViewControllerAnimated:YES completion:nil];
  6. /* 此处参数 info 是一个字典,下面是字典中的键值 (从相机获取的图片和相册获取的图片时,两者的info值不尽相同)
  7. * UIImagePickerControllerMediaType; // 媒体类型
  8. * UIImagePickerControllerOriginalImage; // 原始图片
  9. * UIImagePickerControllerEditedImage; // 裁剪后图片
  10. * UIImagePickerControllerCropRect; // 图片裁剪区域(CGRect)
  11. * UIImagePickerControllerMediaURL; // 媒体的URL
  12. * UIImagePickerControllerReferenceURL // 原件的URL
  13. * UIImagePickerControllerMediaMetadata // 当数据来源是相机时,此值才有效
  14. */
  15. // 从info中将图片取出,并加载到imageView当中
  16. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  17. self.imageView.image = image;
  18. // 创建保存图像时需要传入的选择器对象(回调方法格式固定)
  19. SEL selectorToCall = @selector(image:didFinishSavingWithError:contextInfo:);
  20. // 将图像保存到相册(第三个参数需要传入上面格式的选择器对象)
  21. UIImageWriteToSavedPhotosAlbum(image, self, selectorToCall, NULL);
  22. }
  23.  
  24. // 取消选取调用的方法
  25. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  26. [self dismissViewControllerAnimated:YES completion:nil];
  27. }
  28.  

添加保存图片完成后的回调方法:

  1. // 保存图片后到相册后,回调的相关方法,查看是否保存成功
  2. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  3. if (error == nil){
  4. NSLog(@"Image was saved successfully.");
  5. } else {
  6. NSLog(@"An error happened while saving the image.");
  7. NSLog(@"Error = %@", error);
  8. }
  9. }

 2-3.效果展示

IMG_0125

 

点击选择图片按钮:

IMG_0126

 

使用相机拍摄:

IMG_0127IMG_0128

IMG_0130

 

从图库中选取:

IMG_0134IMG_0131

 

点击choose选择该图片:

IMG_0133

2016-11-02 15:30:00 weixin_30551947 阅读数 4
  • iOS开发-全面解析iOS蓝牙BLE4.0开发

    只要你会OC基础,会写HelloWorld,你就可以实现iOS的蓝牙通信功能,实现蓝牙小项目也不在话下,作者会带领大家详细分析BLE4.0原理,通过分析xcode使用的蓝牙API,让学者能够得心应手的实现蓝牙BLE4.0的开发

    3668 人正在学习 去看看 许英俊

权限分类

  • 联网权限

  • 相册权限

  • 相机、麦克风权限

  • 定位权限

  • 推送权限

  • 通讯录权限

  • 日历、备忘录权限

联网权限

引入头文件 @import CoreTelephony;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
  };
};

查询应用是否有联网功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;

检查是否有相册权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
  case ALAuthorizationStatusAuthorized:
      NSLog(@"Authorized");
      break;
  case ALAuthorizationStatusDenied:
      NSLog(@"Denied");
      break;
  case ALAuthorizationStatusNotDetermined:
      NSLog(@"not Determined");
      break;
  case ALAuthorizationStatusRestricted:
      NSLog(@"Restricted");
      break;
       
  default:
      break;
}

相册权限--iOS 8.0之后

导入头文件@import Photos;

检查是否有相册权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 . . .]获取相册权限

1
2
3
4
5
6
7
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  if (status == PHAuthorizationStatusAuthorized) {
      NSLog(@"Authorized");
  }else{
      NSLog(@"Denied or Restricted");
  }
  }];

相机和麦克风权限

导入头文件@import AVFoundation;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
}

获取相机或麦克风权限

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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中进行配置;

配置文件

检查是否有定位权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 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;
}

获取定位权限

1
2
3
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直获取定位信息
[manager requestWhenInUseAuthorization];//使用的时候获取定位信息

在代理方法中查看权限是否改变

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (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;
  }
   
}

推送权限

检查是否有通讯权限

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}

 

获取推送权限

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

通讯录权限

iOS 9.0之前

导入头文件 @import AddressBook;

检查是否有通讯录权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}

获取通讯录权限

1
2
3
4
5
6
7
8
9
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
  if (granted) {
      NSLog(@"Authorized");
      CFRelease(addressBook);
  }else{
      NSLog(@"Denied or Restricted");
  }
});

iOS 9.0及以后

导入头文件 @import Contacts;

检查是否有通讯录权限

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  switch (status) {
        case CNAuthorizationStatusAuthorized:
        {
            NSLog(@"Authorized:");
        }
            break;
        case CNAuthorizationStatusDenied:{
            NSLog(@"Denied");
        }
            break;
        case CNAuthorizationStatusRestricted:{
            NSLog(@"Restricted");
        }
            break;
        case CNAuthorizationStatusNotDetermined:{
             NSLog(@"NotDetermined");
        }
            break;
             
       }

 

获取通讯录权限

 

1
2
3
4
5
6
7
8
9
10
CNContactStore *contactStore = [[CNContactStore alloc] init];
    [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
 
           NSLog(@"Authorized");
        }else{
 
           NSLog(@"Denied or Restricted");
        }
    }];

日历、备忘录权限

导入头文件

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

1
2
3
4
 typedef NS_ENUM(NSUInteger, EKEntityType) {
  EKEntityTypeEvent,//日历
  EKEntityTypeReminder //备忘
 };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}

获取日历或备忘录权限

1
2
3
4
5
6
7
8
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之后,将这些设置都整合在一起,并且可以开启或关闭相应的权限。所有的权限都可以通过下面的方法打开:

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

转载于:https://www.cnblogs.com/weiboyuan/p/6023089.html

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