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
This commit is contained in:
Nick Lockwood 2016-02-22 05:33:31 -08:00 committed by facebook-github-bot-6
parent 7964d80759
commit e6c65102d4
3 changed files with 36 additions and 6 deletions

View File

@ -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 <ShareActionSheetExample />; }
render(): ReactElement {
return <ShareActionSheetExample url="https://code.facebook.com" />;
}
},
{
title: 'Share Local Image',
render(): ReactElement {
return <ShareActionSheetExample url="bunny.png" />;
}
}
];

View File

@ -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,

View File

@ -156,14 +156,26 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
return;
}
NSMutableArray<id /* NSString or NSURL */> *items = [NSMutableArray array];
NSMutableArray<id> *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");