From ddcc2d00ad959ad782c15e6633418b47599383b8 Mon Sep 17 00:00:00 2001 From: Chaitanya Deorukhkar Date: Wed, 12 Jun 2019 17:52:20 +0530 Subject: [PATCH 1/2] Revert "Restores proper image handling for iOS. Reverts changes in https://github.com/alinz/react-native-share-extension/commit/1e98f58991a892fd941a809f9564fb5181693ba0" This reverts commit b330e63108b3901c1a908c7ca2262e489b3f3246. --- ios/ReactNativeShareExtension.m | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/ios/ReactNativeShareExtension.m b/ios/ReactNativeShareExtension.m index ddf1e692..f6a18ce5 100644 --- a/ios/ReactNativeShareExtension.m +++ b/ios/ReactNativeShareExtension.m @@ -68,6 +68,7 @@ - (void)viewDidLoad { } - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void(^)(NSString *value, NSString* contentType, NSException *exception))callback { + @try { NSExtensionItem *item = [context.inputItems firstObject]; NSArray *attachments = item.attachments; @@ -99,10 +100,31 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void( }]; } else if (imageProvider) { [imageProvider loadItemForTypeIdentifier:IMAGE_IDENTIFIER options:nil completionHandler:^(id item, NSError *error) { - NSURL *url = (NSURL *)item; - + + /** + * Save the image to NSTemporaryDirectory(), which cleans itself tri-daily. + * This is necessary as the iOS 11 screenshot editor gives us a UIImage, while + * sharing from Photos and similar apps gives us a URL + * Therefore the solution is to save a UIImage, either way, and return the local path to that temp UIImage + * This path will be sent to React Native and can be processed and accessed RN side. + **/ + + UIImage *sharedImage; + NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"RNSE_TEMP_IMG"]; + NSString *fullPath = [filePath stringByAppendingPathExtension:@"png"]; + + if ([(NSObject *)item isKindOfClass:[UIImage class]]){ + sharedImage = (UIImage *)item; + }else if ([(NSObject *)item isKindOfClass:[NSURL class]]){ + NSURL* url = (NSURL *)item; + NSData *data = [NSData dataWithContentsOfURL:url]; + sharedImage = [UIImage imageWithData:data]; + } + + [UIImagePNGRepresentation(sharedImage) writeToFile:fullPath atomically:YES]; + if(callback) { - callback([url absoluteString], [[[url absoluteString] pathExtension] lowercaseString], nil); + callback(fullPath, [fullPath pathExtension], nil); } }]; } else if (textProvider) { @@ -126,4 +148,6 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void( } } + + @end From 95817f39466d82d03e8d64b6877bd2814db4f6e8 Mon Sep 17 00:00:00 2001 From: Chaitanya Deorukhkar Date: Thu, 13 Jun 2019 12:22:37 +0530 Subject: [PATCH 2/2] Use original file name with extension --- ios/ReactNativeShareExtension.m | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ios/ReactNativeShareExtension.m b/ios/ReactNativeShareExtension.m index f6a18ce5..9c51016c 100644 --- a/ios/ReactNativeShareExtension.m +++ b/ios/ReactNativeShareExtension.m @@ -68,7 +68,6 @@ - (void)viewDidLoad { } - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void(^)(NSString *value, NSString* contentType, NSException *exception))callback { - @try { NSExtensionItem *item = [context.inputItems firstObject]; NSArray *attachments = item.attachments; @@ -108,15 +107,17 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void( * Therefore the solution is to save a UIImage, either way, and return the local path to that temp UIImage * This path will be sent to React Native and can be processed and accessed RN side. **/ - + UIImage *sharedImage; - NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"RNSE_TEMP_IMG"]; - NSString *fullPath = [filePath stringByAppendingPathExtension:@"png"]; - + NSString *filePath = nil; + NSString *fullPath = nil; + if ([(NSObject *)item isKindOfClass:[UIImage class]]){ sharedImage = (UIImage *)item; + fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Image.png"]; }else if ([(NSObject *)item isKindOfClass:[NSURL class]]){ NSURL* url = (NSURL *)item; + fullPath = [NSTemporaryDirectory() stringByAppendingPathComponent:url.lastPathComponent]; NSData *data = [NSData dataWithContentsOfURL:url]; sharedImage = [UIImage imageWithData:data]; } @@ -148,6 +149,4 @@ - (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void( } } - - @end