react-native-cameraroll: iOS - Photo Library Add Only permission throws unknown error native module

Scenario:

  • Fresh app installation
  • Save a photo using the save() method
  • Photo Library Permission will be requested
  • Choose “Keep Add Only”
  • Error will be thrown: unknown error native module
  • The photo is saved to the Photo Library.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 17

Most upvoted comments

Same here, and after some debugging I think this error is thrown when the lib tries to fetch the newly created asset here

L208 - RNCCameraRoll : PHFetchResult<PHAsset *> *createdAsset = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                  options:options];

with only Add only permission createdAsset became a PHUnauthorizedFetchResult That’s why we can still see the photo saved in the Photo Library and the camera-roll lib still throw an error.

EDIT : as a temporary workaround I’m using patch-package to check createdAsset kind

diff --git a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
index 4769d28..5387a31 100644
--- a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
+++ b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
@@ -207,6 +207,13 @@ RCT_EXPORT_METHOD(saveToCameraRoll:(NSURLRequest *)request
         options.fetchLimit = 1;
         PHFetchResult<PHAsset *> *createdAsset = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                   options:options];
+        // workaround waiting for https://github.com/react-native-cameraroll/react-native-cameraroll/issues/575 to be resolved
+        if (![createdAsset isKindOfClass:[PHAsset class]]) {
+          resolve(@{
+            @"node": [NSNull null]
+          });
+          return;
+        }
         if (createdAsset.count < 1) {
           reject(kErrorUnableToSave, nil, nil);
           return;

I tried this comment but didn’t work because if (createdAsset.count < 1) is true and the patched lines are not reached. I ended up skipping fetching assets if access level is “Add Only”.

@react-native-camera-roll/camera-roll@7.8.3

diff --git a/ios/RNCCameraRoll.mm b/ios/RNCCameraRoll.mm
index d63f1a7ca21d0ce18454c1bf1a488a4160768bb6..6d27b1af92148c2674ea25215553ec551c6f49a8 100644
--- a/ios/RNCCameraRoll.mm
+++ b/ios/RNCCameraRoll.mm
@@ -209,6 +209,16 @@ RCT_EXPORT_METHOD(saveToCameraRoll:(NSURLRequest *)request
       }
     } completionHandler:^(BOOL success, NSError *error) {
       if (success) {
+        if (@available(iOS 14, *)) {
+          PHAuthorizationStatus authStatusAddOnly = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelAddOnly];
+          PHAuthorizationStatus authStatusReadWrite = [PHPhotoLibrary authorizationStatusForAccessLevel:PHAccessLevelReadWrite];
+            if(authStatusAddOnly == PHAuthorizationStatusAuthorized && authStatusReadWrite != PHAuthorizationStatusAuthorized) {
+            resolve(@{
+              @"node": [NSNull null]
+            });
+            return;
+          }
+        }
         PHFetchOptions *options = [PHFetchOptions new];
         options.includeHiddenAssets = YES;
         options.includeAllBurstAssets = YES;

The following patch fixed my issue, hopefully it fixes yours too

diff --git a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
index 7d9c63a..8bfc91f 100644
--- a/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
+++ b/node_modules/@react-native-camera-roll/camera-roll/ios/RNCCameraRoll.mm
@@ -207,11 +207,22 @@ static void requestPhotoLibraryAccess(RCTPromiseRejectBlock reject, PhotosAuthor
         options.fetchLimit = 1;
         PHFetchResult<PHAsset *> *createdAsset = [PHAsset fetchAssetsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                   options:options];
+          
         if (createdAsset.count < 1) {
           reject(kErrorUnableToSave, nil, nil);
           return;
         }
-        NSDictionary *dictionary = [self convertAssetToDictionary:[createdAsset firstObject]
+          
+        PHAsset *asset = [createdAsset firstObject];
+
+        // https://github.com/react-native-cameraroll/react-native-cameraroll/issues/575
+        if (![asset isKindOfClass:[PHAsset class]]) {
+          resolve(@{
+            @"node": [NSNull null]
+          });
+          return;
+        }
+        NSDictionary *dictionary = [self convertAssetToDictionary:asset
                                                     includeAlbums:YES
                                                   includeFilename:YES
                                              includeFileExtension:YES

@react-native-camera-roll/camera-roll => 7.4.2


I used this comment as reference, except the condition was wrong since createdAsset is an array of PHFetchResult so the condition always evaluates to false even when the user had provided full access and we had data in createdAsset

if (![createdAsset isKindOfClass:[PHAsset class]]) {

Probably not related but I started getting this error on 7.4.0 on production release only, on simulator it works just fine.

Click me
CrashReporter Key: xxxxx
Hardware Model:     iPhone14,5
Process:            XXXXX
Identifier:         com.xxxxx.ios
Version:            2.1.0
Role:               Foreground
OS Version:         iOS 17.2.1


NSInvalidArgumentException: -[NSNull isEqualToString:]: unrecognized selector sent to instance 0x2029fc970

0  CoreFoundation +0xec698         ___exceptionPreprocess
1  libobjc.A.dylib +0x2bc7c        _objc_exception_throw
2  CoreFoundation +0x17cfd8        -[NSObject(NSObject) doesNotRecognizeSelector:]
3  CoreFoundation +0x31e04         ____forwarding___
4  CoreFoundation +0x17294c        ___forwarding_prep_0___
5  xxxxx +0x81acb4                  __42-[RNCCameraRoll getPhotos:resolve:reject:]_block_invoke_2 (RNCCameraRoll.mm:460:13)
6  CoreFoundation +0x353f0         ___NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__
7  CoreFoundation +0x8eae4         ___NSArrayEnumerate
8  Photos +0x1f5970                -[PHFetchResult enumerateObjectsUsingBlock:]
9  xxxxxx +0x81b5a4                  __42-[RNCCameraRoll getPhotos:resolve:reject:]_block_invoke.178 (RNCCameraRoll.mm:540:13)
10 CoreFoundation +0x353f0         ___NSARRAY_IS_CALLING_OUT_TO_A_BLOCK__
11 CoreFoundation +0x8eae4         ___NSArrayEnumerate
12 Photos +0x1f5970                -[PHFetchResult enumerateObjectsUsingBlock:]
13 xxxxxx +0x81a734                  __42-[RNCCameraRoll getPhotos:resolve:reject:]_block_invoke (RNCCameraRoll.mm:537:9)
14 xxxxxx +0x818b50                  requestPhotoLibraryAccess(void ( block_pointer)(NSString*, NSString*, NSError*), void ( block_pointer)(bool), bool) (RNCCameraRoll.mm)
15 xxxxx +0x81a238                  -[RNCCameraRoll getPhotos:resolve:reject:] (RNCCameraRoll.mm:455:3)
16 CoreFoundation +0x31790         ___invoking___
17 CoreFoundation +0x31228         -[NSInvocation invoke]
18 CoreFoundation +0x31050         -[NSInvocation invokeWithTarget:]
19 xxxxx +0x5e0518                  -[RCTModuleMethod invokeWithBridge:module:arguments:] (RCTModuleMethod.mm:584:3)
20 xxxxx +0x5e261c                  facebook::react::invokeInner(RCTBridge*, RCTModuleData*, unsigned int, folly::dynamic const&, int, (anonymous namespace)::SchedulingContext) (RCTNativeModule.mm:196:17)
21 xxxxx +0x5e226c                  facebook::react::RCTNativeModule::invoke(unsigned int, folly::dynamic&&, int)::$_0::operator()() const (RCTNativeModule.mm:113:7)
22 libdispatch.dylib +0x26a4       __dispatch_call_block_and_release
23 libdispatch.dylib +0x42fc       __dispatch_client_callout
24 libdispatch.dylib +0xb890       __dispatch_lane_serial_drain
25 libdispatch.dylib +0xc3c0       __dispatch_lane_invoke
26 libdispatch.dylib +0x17000      __dispatch_root_queue_drain_deferred_wlh
27 libdispatch.dylib +0x16874      __dispatch_workloop_worker_thread
28 libsystem_pthread.dylib +0x1960 __pthread_wqthread
29 libsystem_pthread.dylib +0x1a00 _start_wqthread