123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- //
- // LBXPermissionPhotos.m
- // LBXKits
- //
- // Created by lbxia on 2017/9/10.
- // Copyright © 2017年 lbx. All rights reserved.
- //
- #import "LBXPermissionPhotos.h"
- #import <Photos/Photos.h>
- #import <AssetsLibrary/AssetsLibrary.h>
- @implementation LBXPermissionPhotos
- + (BOOL)authorized
- {
- return [self authorizationStatus] == 3;
- }
- /**
- photo permission status
- @return
- 0 :NotDetermined
- 1 :Restricted
- 2 :Denied
- 3 :Authorized
- */
- + (NSInteger)authorizationStatus
- {
- if (@available(iOS 8,*))
- {
- return [PHPhotoLibrary authorizationStatus];
- }
- else
- {
- return [ALAssetsLibrary authorizationStatus];
- }
- }
- + (void)authorizeWithCompletion:(void(^)(BOOL granted,BOOL firstTime))completion
- {
- if (@available(iOS 8.0, *)) {
-
- PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
-
- switch (status) {
- case PHAuthorizationStatusAuthorized:
- {
- if (completion) {
- completion(YES,NO);
- }
- }
- break;
- case PHAuthorizationStatusRestricted:
- case PHAuthorizationStatusDenied:
- {
- if (completion) {
- completion(NO,NO);
- }
- }
- break;
- case PHAuthorizationStatusNotDetermined:
- {
- [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(status == PHAuthorizationStatusAuthorized,YES);
- });
- }
- }];
- }
- break;
- default:
- {
- if (completion) {
- completion(NO,NO);
- }
- }
- break;
- }
-
- }else{
-
- ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
- switch (status) {
- case ALAuthorizationStatusAuthorized:
- {
- if (completion) {
- completion(YES, NO);
- }
- }
- break;
- case ALAuthorizationStatusNotDetermined:
- {
- ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
-
- [library enumerateGroupsWithTypes:ALAssetsGroupAll
- usingBlock:^(ALAssetsGroup *assetGroup, BOOL *stop) {
- if (*stop) {
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(YES, NO);
- });
-
- }
- } else {
- *stop = YES;
- }
- }
- failureBlock:^(NSError *error) {
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion(NO, YES);
- });
- }
- }];
- } break;
- case ALAuthorizationStatusRestricted:
- case ALAuthorizationStatusDenied:
- {
- if (completion) {
- completion(NO, NO);
- }
- }
- break;
- }
- }
-
- }
- @end
|