Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit a1bf146

Browse files
author
Sérgio Estêvão
committed
Merge pull request #84 from wordpress-mobile/issue/improve_permission_handling
Improve permission handling errors.
2 parents a09bdb2 + cc7e285 commit a1bf146

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

Pod/Classes/WPALAssetDataSource.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ - (ALAssetsLibrary *)assetsLibrary
9595
- (void)loadDataWithSuccess:(WPMediaChangesBlock)successBlock
9696
failure:(WPMediaFailureBlock)failureBlock
9797
{
98+
ALAuthorizationStatus authorizationStatus = ALAssetsLibrary.authorizationStatus;
99+
if (authorizationStatus == ALAuthorizationStatusDenied ||
100+
authorizationStatus == ALAuthorizationStatusRestricted) {
101+
if (failureBlock) {
102+
NSError *error = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:nil];
103+
failureBlock(error);
104+
}
105+
return;
106+
}
98107
[self.extraAssets removeAllObjects];
99108
if (self.refreshGroups) {
100109
[self loadGroupsWithSuccess:^{
@@ -127,8 +136,14 @@ - (void)loadGroupsWithSuccess:(WPMediaChangesBlock)successBlock
127136
}
128137
} failureBlock:^(NSError *error) {
129138
NSLog(@"Error: %@", [error localizedDescription]);
139+
NSError * filteredError = error;
140+
if ([error.domain isEqualToString:ALAssetsLibraryErrorDomain] &&
141+
(error.code == ALAssetsLibraryAccessUserDeniedError || error.code == ALAssetsLibraryAccessGloballyDeniedError)
142+
) {
143+
filteredError = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:error.userInfo];
144+
}
130145
if (failureBlock) {
131-
failureBlock(error);
146+
failureBlock(filteredError);
132147
}
133148
}];
134149
}

Pod/Classes/WPMediaCollectionDataSource.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ typedef NS_ENUM(NSInteger, WPMediaType){
55
WPMediaTypeAll
66
};
77

8+
static NSString * const WPMediaPickerErrorDomain = @"WPMediaPickerErrorDomain";
9+
10+
typedef NS_ENUM(NSInteger, WPMediaPickerErrorCode){
11+
WPMediaErrorCodePermissionsFailed,
12+
WPMediaErrorCodePermissionsUnknow
13+
};
14+
815
@protocol WPMediaAsset;
916

1017
typedef void (^WPMediaChangesBlock)();

Pod/Classes/WPMediaCollectionViewController.m

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#import "WPMediaCaptureCollectionViewCell.h"
44
#import "WPMediaPickerViewController.h"
55
#import "WPMediaGroupPickerViewController.h"
6-
#import "WPALAssetDataSource.h"
76

87
@import MobileCoreServices;
98
@import AVFoundation;
@@ -274,22 +273,21 @@ - (void)refreshData
274273
strongSelf.collectionView.allowsSelection = YES;
275274
strongSelf.collectionView.scrollEnabled = YES;
276275
[strongSelf.collectionView reloadData];
277-
if ([error.domain isEqualToString:ALAssetsLibraryErrorDomain]) {
278-
if (error.code == ALAssetsLibraryAccessUserDeniedError || error.code == ALAssetsLibraryAccessGloballyDeniedError) {
279-
NSString *otherButtonTitle = nil;
280-
if ([[self class] isiOS8OrAbove]) {
281-
otherButtonTitle = NSLocalizedString(@"Open Settings", @"Go to the settings app");
282-
}
283-
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when access to the media library is not granted by the user")
284-
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.")
285-
delegate:self
286-
cancelButtonTitle:NSLocalizedString(@"OK", "")
287-
otherButtonTitles:otherButtonTitle,nil];
288-
alertView.tag = WPMediaCollectionAlertMediaLibraryPermissionsNeeded;
289-
alertView.delegate = strongSelf;
290-
[alertView show];
291-
return;
276+
if (error.domain == WPMediaPickerErrorDomain &&
277+
error.code == WPMediaErrorCodePermissionsFailed) {
278+
NSString *otherButtonTitle = nil;
279+
if ([[self class] isiOS8OrAbove]) {
280+
otherButtonTitle = NSLocalizedString(@"Open Settings", @"Go to the settings app");
292281
}
282+
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when access to the media library is not granted by the user")
283+
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.")
284+
delegate:self
285+
cancelButtonTitle:NSLocalizedString(@"OK", "")
286+
otherButtonTitles:otherButtonTitle,nil];
287+
alertView.tag = WPMediaCollectionAlertMediaLibraryPermissionsNeeded;
288+
alertView.delegate = strongSelf;
289+
[alertView show];
290+
return;
293291
}
294292
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Media Library", @"Title for alert when a generic error happened when loading media")
295293
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.")

Pod/Classes/WPPHAssetDataSource.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,24 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance
6969
- (void)loadDataWithSuccess:(WPMediaChangesBlock)successBlock
7070
failure:(WPMediaFailureBlock)failureBlock
7171
{
72-
if (self.refreshGroups) {
73-
[self loadGroupsWithSuccess:^{
74-
self.refreshGroups = NO;
72+
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
73+
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusDenied ||
74+
[PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusRestricted) {
75+
if (failureBlock) {
76+
NSError *error = [NSError errorWithDomain:WPMediaPickerErrorDomain code:WPMediaErrorCodePermissionsFailed userInfo:nil];
77+
failureBlock(error);
78+
}
79+
return;
80+
}
81+
if (self.refreshGroups) {
82+
[self loadGroupsWithSuccess:^{
83+
self.refreshGroups = NO;
84+
[self loadAssetsWithSuccess:successBlock failure:failureBlock];
85+
} failure:failureBlock];
86+
} else {
7587
[self loadAssetsWithSuccess:successBlock failure:failureBlock];
76-
} failure:failureBlock];
77-
} else {
78-
[self loadAssetsWithSuccess:successBlock failure:failureBlock];
79-
}
88+
}
89+
}];
8090
}
8191

8292
- (void)loadGroupsWithSuccess:(WPMediaChangesBlock)successBlock

0 commit comments

Comments
 (0)