Skip to content

Commit

Permalink
feat(iOS14): Support for the new auth status `PHAuthorizationStatusLi…
Browse files Browse the repository at this point in the history
…mited` from iOS 14 (#326)

* feat `limited` in the response of CameraRoll.getPhotos(...) if the permission is `PHAuthorizationStatusLimited`

* update Flow types

* add example usage of the limited info
  • Loading branch information
drash-course committed Oct 10, 2021
1 parent 1421c1d commit 3ade8c0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Returns a Promise which when resolved will be of the following shape:
* `has_next_page`: {boolean}
* `start_cursor`: {string}
* `end_cursor`: {string}
* `limited` : {boolean | undefined} : true if the app can only access a subset of the gallery pictures (authorization is `PHAuthorizationStatusLimited`), false otherwise (iOS only)

#### Example

Expand Down
20 changes: 19 additions & 1 deletion example/js/CameraRollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const {
Platform,
StyleSheet,
View,
TouchableOpacity,
Text,
Linking,
} = ReactNative;

import CameraRoll from '../../js/CameraRoll';
Expand Down Expand Up @@ -61,6 +64,7 @@ class CameraRollView extends React.Component {
lastCursor: null,
noMore: false,
loadingMore: false,
isLimited: false,
};
}

Expand Down Expand Up @@ -148,6 +152,16 @@ class CameraRollView extends React.Component {
if (!this.state.noMore) {
return <ActivityIndicator />;
}
if (this.state.isLimited) {
return (
<TouchableOpacity onPress={Linking.openSettings}>
<Text style={styles.footerText}>
Not all pictures are available. Tap here to go to Settings and
change which media the app is allowed to access.
</Text>
</TouchableOpacity>
);
}
return null;
};

Expand All @@ -162,7 +176,7 @@ class CameraRollView extends React.Component {

_appendAssets(data) {
const assets = data.edges;
const newState = {loadingMore: false};
const newState = {loadingMore: false, isLimited: data.limited};

if (!data.page_info.has_next_page) {
newState.noMore = true;
Expand Down Expand Up @@ -210,6 +224,10 @@ const styles = StyleSheet.create({
container: {
flex: 1,
},
footerText: {
padding: 20,
textAlign: 'center',
},
});

module.exports = CameraRollView;
69 changes: 44 additions & 25 deletions ios/RNCCameraRollManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ + (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
NSString *const lowercase = [mediaType lowercaseString];
NSMutableArray *format = [NSMutableArray new];
NSMutableArray *arguments = [NSMutableArray new];

if ([lowercase isEqualToString:@"photos"]) {
[format addObject:@"mediaType = %d"];
[arguments addObject:@(PHAssetMediaTypeImage)];
Expand All @@ -62,7 +62,7 @@ + (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
"'videos' or 'all'.", mediaType);
}
}

if (fromTime > 0) {
NSDate* fromDate = [NSDate dateWithTimeIntervalSince1970:fromTime/1000];
[format addObject:@"creationDate > %@"];
Expand All @@ -73,7 +73,7 @@ + (PHFetchOptions *)PHFetchOptionsFromMediaType:(NSString *)mediaType
[format addObject:@"creationDate <= %@"];
[arguments addObject:toDate];
}

// This case includes the "all" mediatype
PHFetchOptions *const options = [PHFetchOptions new];
if ([format count] > 0) {
Expand All @@ -96,18 +96,34 @@ @implementation RNCCameraRollManager
static NSString *const kErrorAuthRestricted = @"E_PHOTO_LIBRARY_AUTH_RESTRICTED";
static NSString *const kErrorAuthDenied = @"E_PHOTO_LIBRARY_AUTH_DENIED";

typedef void (^PhotosAuthorizedBlock)(void);
typedef void (^PhotosAuthorizedBlock)(bool isLimited);

static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthorizedBlock authorizedBlock) {
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
PHAuthorizationStatus authStatus;
if (@available(iOS 14, *)) {
authStatus = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
} else {
authStatus = [PHPhotoLibrary authorizationStatus];
}
if (authStatus == PHAuthorizationStatusRestricted) {
reject(kErrorAuthRestricted, @"Access to photo library is restricted", nil);
} else if (authStatus == PHAuthorizationStatusAuthorized) {
authorizedBlock();
authorizedBlock(false);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
} else if (authStatus == PHAuthorizationStatusLimited) {
#pragma clang diagnostic pop
authorizedBlock(true);
} else if (authStatus == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
requestPhotoLibraryAccess(reject, authorizedBlock);
}];
if (@available(iOS 14, *)) {
[PHPhotoLibrary requestAuthorizationForAccessLevel:PHAccessLevelReadWrite handler:^(PHAuthorizationStatus status) {
requestPhotoLibraryAccess(reject, authorizedBlock);
}];
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
requestPhotoLibraryAccess(reject, authorizedBlock);
}];
}
} else {
reject(kErrorAuthDenied, @"Access to photo library was denied", nil);
}
Expand Down Expand Up @@ -159,7 +175,7 @@ static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthor
};
void (^saveWithOptions)(void) = ^void() {
if (![options[@"album"] isEqualToString:@""]) {

PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", options[@"album"] ];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
Expand Down Expand Up @@ -188,7 +204,7 @@ static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthor
}
};

void (^loadBlock)(void) = ^void() {
void (^loadBlock)(bool isLimited) = ^void(bool isLimited) {
inputURI = request.URL;
saveWithOptions();
};
Expand Down Expand Up @@ -220,14 +236,16 @@ static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthor

static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
NSArray<NSDictionary<NSString *, id> *> *assets,
BOOL hasNextPage)
BOOL hasNextPage,
bool isLimited)
{
if (!assets.count) {
resolve(@{
@"edges": assets,
@"page_info": @{
@"has_next_page": @NO,
}
},
@"limited": @(isLimited)
});
return;
}
Expand All @@ -237,7 +255,8 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
@"start_cursor": assets[0][@"node"][@"image"][@"uri"],
@"end_cursor": assets[assets.count - 1][@"node"][@"image"][@"uri"],
@"has_next_page": @(hasNextPage),
}
},
@"limited": @(isLimited)
});
}

Expand All @@ -262,14 +281,14 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
BOOL __block includeLocation = [include indexOfObject:@"location"] != NSNotFound;
BOOL __block includeImageSize = [include indexOfObject:@"imageSize"] != NSNotFound;
BOOL __block includePlayableDuration = [include indexOfObject:@"playableDuration"] != NSNotFound;

// If groupTypes is "all", we want to fetch the SmartAlbum "all photos". Otherwise, all
// other groupTypes values require the "album" collection type.
PHAssetCollectionType const collectionType = ([groupTypes isEqualToString:@"all"]
? PHAssetCollectionTypeSmartAlbum
: PHAssetCollectionTypeAlbum);
PHAssetCollectionSubtype const collectionSubtype = [RCTConvert PHAssetCollectionSubtype:groupTypes];

// Predicate for fetching assets within a collection
PHFetchOptions *const assetFetchOptions = [RCTConvert PHFetchOptionsFromMediaType:mediaType fromTime:fromTime toTime:toTime];
// We can directly set the limit if we guarantee every image fetched will be
Expand All @@ -285,29 +304,29 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
assetFetchOptions.fetchLimit = first + 1;
}
assetFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];

BOOL __block foundAfter = NO;
BOOL __block hasNextPage = NO;
BOOL __block resolvedPromise = NO;
NSMutableArray<NSDictionary<NSString *, id> *> *assets = [NSMutableArray new];

// Filter collection name ("group")
PHFetchOptions *const collectionFetchOptions = [PHFetchOptions new];
collectionFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"endDate" ascending:NO]];
if (groupName != nil) {
collectionFetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", groupName];
}

BOOL __block stopCollections_;
NSString __block *currentCollectionName;

requestPhotoLibraryAccess(reject, ^{
requestPhotoLibraryAccess(reject, ^(bool isLimited){
void (^collectAsset)(PHAsset*, NSUInteger, BOOL*) = ^(PHAsset * _Nonnull asset, NSUInteger assetIdx, BOOL * _Nonnull stopAssets) {
NSString *const uri = [NSString stringWithFormat:@"ph://%@", [asset localIdentifier]];
NSString *_Nullable originalFilename = NULL;
PHAssetResource *_Nullable resource = NULL;
NSNumber* fileSize = [NSNumber numberWithInt:0];

if (includeFilename || includeFileSize || [mimeTypes count] > 0) {
// Get underlying resources of an asset - this includes files as well as details about edited PHAssets
// This is required for the filename and mimeType filtering
Expand All @@ -316,7 +335,7 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
originalFilename = resource.originalFilename;
fileSize = [resource valueForKey:@"fileSize"];
}

// WARNING: If you add any code to `collectAsset` that may skip adding an
// asset to the `assets` output array, you should do it inside this
// block and ensure the logic for `collectAssetMayOmitAsset` above is
Expand Down Expand Up @@ -354,7 +373,7 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
stopCollections_ = YES;
hasNextPage = YES;
RCTAssert(resolvedPromise == NO, @"Resolved the promise before we finished processing the results.");
RCTResolvePromise(resolve, assets, hasNextPage);
RCTResolvePromise(resolve, assets, hasNextPage, isLimited);
resolvedPromise = YES;
return;
}
Expand Down Expand Up @@ -412,7 +431,7 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
// If we get this far and haven't resolved the promise yet, we reached the end of the list of photos
if (!resolvedPromise) {
hasNextPage = NO;
RCTResolvePromise(resolve, assets, hasNextPage);
RCTResolvePromise(resolve, assets, hasNextPage, isLimited);
resolvedPromise = YES;
}
});
Expand All @@ -423,7 +442,7 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
reject:(RCTPromiseRejectBlock)reject)
{
NSMutableArray *convertedAssets = [NSMutableArray array];

for (NSString *asset in assets) {
[convertedAssets addObject: [asset stringByReplacingOccurrencesOfString:@"ph://" withString:@""]];
}
Expand Down
2 changes: 2 additions & 0 deletions js/CameraRoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ export type PhotoIdentifiersPage = {
start_cursor?: string,
end_cursor?: string,
},
limited?: boolean,
};

export type SaveToCameraRollOptions = {
type?: 'photo' | 'video' | 'auto',
album?: string,
Expand Down

0 comments on commit 3ade8c0

Please sign in to comment.