Skip to content

[iOS]: paste after 30min being "copied" makes the app crashes聽#43

Description

@laurentlouk

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Today I used patch-package to patch react-native-image-keyboard@2.2.1 for the project I'm working on.

The issue I have is when I copy an image from the photo library on iOS then wait for 30 minutes and paste it into a TextInput the app crashes (Physical device production/debug and Virtual Device).

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m b/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
index 90b89d9..f9ea98f 100644
--- a/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
+++ b/node_modules/react-native-image-keyboard/ios/RCTBaseTextInputView+MediaInput.m
@@ -68,31 +68,40 @@ static NSArray *acceptedTypes;
             image = [self extractImageFromPasteboard];
         }
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-            NSString *mimeType = [image mimeTypeByGuessingFromData];
-            NSString *fileExtension = [mimeType stringByReplacingOccurrencesOfString:@"image/"
-                                                                          withString:@""];
-
-            NSString *base64 = [image base64EncodedStringWithOptions:0];
-
-            NSArray<NSString*> *paths = NSSearchPathForDirectoriesInDomains(
-                                                                            NSDocumentDirectory,
-                                                                            NSUserDomainMask,
-                                                                            YES);
-            NSUUID *uuid = [NSUUID UUID];
-            NSString *uniqueFileName = [uuid UUIDString];
-            NSString *path = [NSString stringWithFormat:@"%@/%@.%@",
-                              paths[0],
-                              uniqueFileName,
-                              fileExtension];
-
-            [image writeToFile:path atomically:YES];
-
-            NSLog(@"%@", path);
-            self.onImageChange(@{
-                @"data": base64,
-                @"uri": path,
-                @"mime": mimeType,
-                              });
+            if (image) {
+                NSString *mimeType = [image mimeTypeByGuessingFromData];
+                if (mimeType) {
+                    NSString *fileExtension = [mimeType stringByReplacingOccurrencesOfString:@"image/" withString:@""];
+                    NSString *base64 = [image base64EncodedStringWithOptions:0];
+
+                    NSArray<NSString*> *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+                    NSUUID *uuid = [NSUUID UUID];
+                    NSString *uniqueFileName = [uuid UUIDString];
+                    NSString *path = [NSString stringWithFormat:@"%@/%@.%@", paths[0], uniqueFileName, fileExtension];
+
+                    NSError *error = nil;
+                    [image writeToFile:path options:NSDataWritingAtomic error:&error];
+                    if (!error) {
+                        NSLog(@"%@", path);
+                        self.onImageChange(@{@"data": base64, @"uri": path, @"mime": mimeType});
+                    } else {
+                        NSLog(@"Error writing file: %@", error);
+
+                        // Call the normal paste action as fallback
+                        [[self backedTextInputView] paste:sender];
+                    }
+                } else {
+                    NSLog(@"Error: MIME type is nil");
+
+                    // Call the normal paste action as fallback
+                    [[self backedTextInputView] paste:sender];
+                }
+            } else {
+                NSLog(@"Error: Image data is nil");
+
+                // Call the normal paste action as fallback
+                [[self backedTextInputView] paste:sender];
+            }
         });
     } else {
         // Call the normal paste action

This issue body was partially generated by patch-package.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions