From e6c65102d4b1e3b7509a2fc4d595da51e83c6b77 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Mon, 22 Feb 2016 05:33:31 -0800 Subject: [PATCH] Add support for sharing images or other media via iOS share sheet Summary:`ActionSheetIOS` now supports sharing images or other media via the `showShareActionSheetWithOptions` method. Simply specify a local file or data uri using the `url` argument to share the file. NOTE: this mechanism doesn't currently support sharing images from the camera roll, however you can work around this by first saving the image to the ImageStore, and then fetching the base64 data. Reviewed By: javache Differential Revision: D2954273 fb-gh-sync-id: d5158f9d167fe92199933ca703f40f561732ac37 shipit-source-id: d5158f9d167fe92199933ca703f40f561732ac37 --- Examples/UIExplorer/ActionSheetIOSExample.js | 13 ++++++++++--- Libraries/ActionSheetIOS/ActionSheetIOS.js | 13 ++++++++++++- Libraries/ActionSheetIOS/RCTActionSheetManager.m | 16 ++++++++++++++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Examples/UIExplorer/ActionSheetIOSExample.js b/Examples/UIExplorer/ActionSheetIOSExample.js index ccc86fba4..31fd79c38 100644 --- a/Examples/UIExplorer/ActionSheetIOSExample.js +++ b/Examples/UIExplorer/ActionSheetIOSExample.js @@ -98,7 +98,6 @@ var ActionSheetTintExample = React.createClass({ } }); - var ShareActionSheetExample = React.createClass({ getInitialState() { return { @@ -121,7 +120,7 @@ var ShareActionSheetExample = React.createClass({ showShareActionSheet() { ActionSheetIOS.showShareActionSheetWithOptions({ - url: 'https://code.facebook.com', + url: this.props.url, message: 'message to go with the shared url', subject: 'a subject to go in the email heading', excludedActivityTypes: [ @@ -163,6 +162,14 @@ exports.examples = [ }, { title: 'Show Share Action Sheet', - render(): ReactElement { return ; } + render(): ReactElement { + return ; + } + }, + { + title: 'Share Local Image', + render(): ReactElement { + return ; + } } ]; diff --git a/Libraries/ActionSheetIOS/ActionSheetIOS.js b/Libraries/ActionSheetIOS/ActionSheetIOS.js index c63618441..9570d2b8c 100644 --- a/Libraries/ActionSheetIOS/ActionSheetIOS.js +++ b/Libraries/ActionSheetIOS/ActionSheetIOS.js @@ -31,7 +31,18 @@ var ActionSheetIOS = { callback ); }, - + + /** + * Display the iOS share sheet. The `options` object should contain + * one or both of: + * + * - `message` (string) - a message to share + * - `url` (string) - a URL to share + * + * NOTE: if `url` points to a local file, or is a base64-encoded + * uri, the file it points to will be loaded and shared directly. + * In this way, you can share images, videos, PDF files, etc. + */ showShareActionSheetWithOptions( options: Object, failureCallback: Function, diff --git a/Libraries/ActionSheetIOS/RCTActionSheetManager.m b/Libraries/ActionSheetIOS/RCTActionSheetManager.m index ab1a8c8d0..18fc8596b 100644 --- a/Libraries/ActionSheetIOS/RCTActionSheetManager.m +++ b/Libraries/ActionSheetIOS/RCTActionSheetManager.m @@ -156,14 +156,26 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options return; } - NSMutableArray *items = [NSMutableArray array]; + NSMutableArray *items = [NSMutableArray array]; NSString *message = [RCTConvert NSString:options[@"message"]]; if (message) { [items addObject:message]; } NSURL *URL = [RCTConvert NSURL:options[@"url"]]; if (URL) { - [items addObject:URL]; + if (URL.fileURL || [URL.scheme.lowercaseString isEqualToString:@"data"]) { + NSError *error; + NSData *data = [NSData dataWithContentsOfURL:URL + options:(NSDataReadingOptions)0 + error:&error]; + if (!data) { + failureCallback(error); + return; + } + [items addObject:data]; + } else { + [items addObject:URL]; + } } if (items.count == 0) { RCTLogError(@"No `url` or `message` to share");