Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
laurentlouk opened this issue Jan 19, 2024 · 1 comment
Open

Comments

@laurentlouk
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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.

@mowaisch
Copy link

I faced this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants