diff --git a/Pod/Classes/WPALAssetDataSource.m b/Pod/Classes/WPALAssetDataSource.m index 9e6b95e0..824f4553 100644 --- a/Pod/Classes/WPALAssetDataSource.m +++ b/Pod/Classes/WPALAssetDataSource.m @@ -95,6 +95,15 @@ - (ALAssetsLibrary *)assetsLibrary - (void)loadDataWithSuccess:(WPMediaChangesBlock)successBlock failure:(WPMediaFailureBlock)failureBlock { + ALAuthorizationStatus authorizationStatus = ALAssetsLibrary.authorizationStatus; + if (authorizationStatus == ALAuthorizationStatusDenied || + authorizationStatus == ALAuthorizationStatusRestricted) { + if (failureBlock) { + NSError *error = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:nil]; + failureBlock(error); + } + return; + } [self.extraAssets removeAllObjects]; if (self.refreshGroups) { [self loadGroupsWithSuccess:^{ @@ -127,8 +136,14 @@ - (void)loadGroupsWithSuccess:(WPMediaChangesBlock)successBlock } } failureBlock:^(NSError *error) { NSLog(@"Error: %@", [error localizedDescription]); + NSError * filteredError = error; + if ([error.domain isEqualToString:ALAssetsLibraryErrorDomain] && + (error.code == ALAssetsLibraryAccessUserDeniedError || error.code == ALAssetsLibraryAccessGloballyDeniedError) + ) { + filteredError = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:error.userInfo]; + } if (failureBlock) { - failureBlock(error); + failureBlock(filteredError); } }]; } diff --git a/Pod/Classes/WPMediaCollectionDataSource.h b/Pod/Classes/WPMediaCollectionDataSource.h index 7054cad6..d1e2ebb6 100644 --- a/Pod/Classes/WPMediaCollectionDataSource.h +++ b/Pod/Classes/WPMediaCollectionDataSource.h @@ -5,6 +5,13 @@ typedef NS_ENUM(NSInteger, WPMediaType){ WPMediaTypeAll }; +static NSString * const WPMediaPickerErrorDomain = @"WPMediaPickerErrorDomain"; + +typedef NS_ENUM(NSInteger, WPMediaPickerErrorCode){ + WPMediaErrorCodePermissionsFailed, + WPMediaErrorCodePermissionsUnknow +}; + @protocol WPMediaAsset; typedef void (^WPMediaChangesBlock)(); diff --git a/Pod/Classes/WPMediaCollectionViewController.m b/Pod/Classes/WPMediaCollectionViewController.m index 07f90c41..88d04704 100644 --- a/Pod/Classes/WPMediaCollectionViewController.m +++ b/Pod/Classes/WPMediaCollectionViewController.m @@ -3,7 +3,6 @@ #import "WPMediaCaptureCollectionViewCell.h" #import "WPMediaPickerViewController.h" #import "WPMediaGroupPickerViewController.h" -#import "WPALAssetDataSource.h" @import MobileCoreServices; @import AVFoundation; @@ -274,22 +273,21 @@ - (void)refreshData strongSelf.collectionView.allowsSelection = YES; strongSelf.collectionView.scrollEnabled = YES; [strongSelf.collectionView reloadData]; - if ([error.domain isEqualToString:ALAssetsLibraryErrorDomain]) { - if (error.code == ALAssetsLibraryAccessUserDeniedError || error.code == ALAssetsLibraryAccessGloballyDeniedError) { - NSString *otherButtonTitle = nil; - if ([[self class] isiOS8OrAbove]) { - otherButtonTitle = NSLocalizedString(@"Open Settings", @"Go to the settings app"); - } - UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when access to the media library is not granted by the user") - message:NSLocalizedString(@"This app needs permission to access your device media library in order to add photos and/or video to your posts. Please change the privacy settings if you wish to allow this.", @"Explaining to the user why the app needs access to the device media library.") - delegate:self - cancelButtonTitle:NSLocalizedString(@"OK", "") - otherButtonTitles:otherButtonTitle,nil]; - alertView.tag = WPMediaCollectionAlertMediaLibraryPermissionsNeeded; - alertView.delegate = strongSelf; - [alertView show]; - return; + if (error.domain == WPMediaPickerErrorDomain && + error.code == WPMediaErrorCodePermissionsFailed) { + NSString *otherButtonTitle = nil; + if ([[self class] isiOS8OrAbove]) { + otherButtonTitle = NSLocalizedString(@"Open Settings", @"Go to the settings app"); } + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when access to the media library is not granted by the user") + message:NSLocalizedString(@"This app needs permission to access your device media library in order to add photos and/or video to your posts. Please change the privacy settings if you wish to allow this.", @"Explaining to the user why the app needs access to the device media library.") + delegate:self + cancelButtonTitle:NSLocalizedString(@"OK", "") + otherButtonTitles:otherButtonTitle,nil]; + alertView.tag = WPMediaCollectionAlertMediaLibraryPermissionsNeeded; + alertView.delegate = strongSelf; + [alertView show]; + return; } UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when a generic error happened when loading media") message:NSLocalizedString(@"There was a problem when trying to access your media. Please try again later.", @"Explaining to the user there was an generic error accesing media.") diff --git a/Pod/Classes/WPPHAssetDataSource.m b/Pod/Classes/WPPHAssetDataSource.m index b45822ea..62796f54 100644 --- a/Pod/Classes/WPPHAssetDataSource.m +++ b/Pod/Classes/WPPHAssetDataSource.m @@ -69,14 +69,24 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance - (void)loadDataWithSuccess:(WPMediaChangesBlock)successBlock failure:(WPMediaFailureBlock)failureBlock { - if (self.refreshGroups) { - [self loadGroupsWithSuccess:^{ - self.refreshGroups = NO; + [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { + if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusDenied || + [PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusRestricted) { + if (failureBlock) { + NSError *error = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:nil]; + failureBlock(error); + } + return; + } + if (self.refreshGroups) { + [self loadGroupsWithSuccess:^{ + self.refreshGroups = NO; + [self loadAssetsWithSuccess:successBlock failure:failureBlock]; + } failure:failureBlock]; + } else { [self loadAssetsWithSuccess:successBlock failure:failureBlock]; - } failure:failureBlock]; - } else { - [self loadAssetsWithSuccess:successBlock failure:failureBlock]; - } + } + }]; } - (void)loadGroupsWithSuccess:(WPMediaChangesBlock)successBlock