2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 20:28:42 +00:00
|
|
|
*/
|
2015-03-13 17:45:27 +00:00
|
|
|
|
|
|
|
#import "RCTActionSheetManager.h"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTBridge.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTUIManager.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
@interface RCTActionSheetManager () <UIActionSheetDelegate>
|
2015-03-13 17:45:27 +00:00
|
|
|
@end
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
@implementation RCTActionSheetManager
|
|
|
|
{
|
2015-10-21 19:19:33 +00:00
|
|
|
// Use NSMapTable, as UIAlertViews do not implement <NSCopying>
|
|
|
|
// which is required for NSDictionary keys
|
|
|
|
NSMapTable *_callbacks;
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2016-06-04 23:25:55 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
2015-10-21 19:19:33 +00:00
|
|
|
return dispatch_get_main_queue();
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
/*
|
|
|
|
* The `anchor` option takes a view to set as the anchor for the share
|
|
|
|
* popup to point to, on iPads running iOS 8. If it is not passed, it
|
|
|
|
* defaults to centering the share popup on screen without any arrows.
|
|
|
|
*/
|
|
|
|
- (CGRect)sourceRectInView:(UIView *)sourceView
|
|
|
|
anchorViewTag:(NSNumber *)anchorViewTag
|
2015-04-20 19:06:02 +00:00
|
|
|
{
|
2015-10-21 19:19:33 +00:00
|
|
|
if (anchorViewTag) {
|
|
|
|
UIView *anchorView = [self.bridge.uiManager viewForReactTag:anchorViewTag];
|
|
|
|
return [anchorView convertRect:anchorView.bounds toView:sourceView];
|
|
|
|
} else {
|
|
|
|
return (CGRect){sourceView.center, {1, 1}};
|
|
|
|
}
|
2015-04-20 19:06:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(showActionSheetWithOptions:(NSDictionary *)options
|
2015-10-21 19:19:33 +00:00
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
RCTLogError(@"Unable to show action sheet from app extension");
|
|
|
|
return;
|
|
|
|
}
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
if (!_callbacks) {
|
|
|
|
_callbacks = [NSMapTable strongToStrongObjectsMapTable];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
NSString *title = [RCTConvert NSString:options[@"title"]];
|
2016-03-31 05:10:00 +00:00
|
|
|
NSString *message = [RCTConvert NSString:options[@"message"]];
|
2015-11-03 22:45:46 +00:00
|
|
|
NSArray<NSString *> *buttons = [RCTConvert NSStringArray:options[@"options"]];
|
2015-10-21 19:19:33 +00:00
|
|
|
NSInteger destructiveButtonIndex = options[@"destructiveButtonIndex"] ? [RCTConvert NSInteger:options[@"destructiveButtonIndex"]] : -1;
|
|
|
|
NSInteger cancelButtonIndex = options[@"cancelButtonIndex"] ? [RCTConvert NSInteger:options[@"cancelButtonIndex"]] : -1;
|
|
|
|
|
2016-05-21 00:03:34 +00:00
|
|
|
UIViewController *controller = RCTPresentedViewController();
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
if (controller == nil) {
|
|
|
|
RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options);
|
|
|
|
return;
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
/*
|
|
|
|
* The `anchor` option takes a view to set as the anchor for the share
|
|
|
|
* popup to point to, on iPads running iOS 8. If it is not passed, it
|
|
|
|
* defaults to centering the share popup on screen without any arrows.
|
|
|
|
*/
|
|
|
|
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
|
|
|
|
UIView *sourceView = controller.view;
|
|
|
|
CGRect sourceRect = [self sourceRectInView:sourceView anchorViewTag:anchorViewTag];
|
|
|
|
|
2016-09-02 02:37:54 +00:00
|
|
|
UIAlertController *alertController =
|
|
|
|
[UIAlertController alertControllerWithTitle:title
|
|
|
|
message:message
|
|
|
|
preferredStyle:UIAlertControllerStyleActionSheet];
|
|
|
|
|
|
|
|
NSInteger index = 0;
|
|
|
|
for (NSString *option in buttons) {
|
|
|
|
UIAlertActionStyle style = UIAlertActionStyleDefault;
|
|
|
|
if (index == destructiveButtonIndex) {
|
|
|
|
style = UIAlertActionStyleDestructive;
|
|
|
|
} else if (index == cancelButtonIndex) {
|
|
|
|
style = UIAlertActionStyleCancel;
|
2015-10-21 19:19:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 02:37:54 +00:00
|
|
|
NSInteger localIndex = index;
|
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:option
|
|
|
|
style:style
|
|
|
|
handler:^(__unused UIAlertAction *action){
|
|
|
|
callback(@[@(localIndex)]);
|
|
|
|
}]];
|
2015-10-21 19:19:33 +00:00
|
|
|
|
2016-09-02 02:37:54 +00:00
|
|
|
index++;
|
|
|
|
}
|
2015-12-09 13:08:36 +00:00
|
|
|
|
2016-09-02 02:37:54 +00:00
|
|
|
alertController.modalPresentationStyle = UIModalPresentationPopover;
|
|
|
|
alertController.popoverPresentationController.sourceView = sourceView;
|
|
|
|
alertController.popoverPresentationController.sourceRect = sourceRect;
|
|
|
|
if (!anchorViewTag) {
|
|
|
|
alertController.popoverPresentationController.permittedArrowDirections = 0;
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2016-09-02 02:37:54 +00:00
|
|
|
[controller presentViewController:alertController animated:YES completion:nil];
|
|
|
|
|
|
|
|
alertController.view.tintColor = [RCTConvert UIColor:options[@"tintColor"]];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
|
2015-10-21 19:19:33 +00:00
|
|
|
failureCallback:(RCTResponseErrorBlock)failureCallback
|
2015-04-08 15:52:48 +00:00
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback)
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
2015-10-21 19:19:33 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
RCTLogError(@"Unable to show action sheet from app extension");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-22 13:33:31 +00:00
|
|
|
NSMutableArray<id> *items = [NSMutableArray array];
|
2015-08-04 23:18:44 +00:00
|
|
|
NSString *message = [RCTConvert NSString:options[@"message"]];
|
|
|
|
if (message) {
|
2015-04-18 17:43:20 +00:00
|
|
|
[items addObject:message];
|
|
|
|
}
|
2017-12-11 18:37:14 +00:00
|
|
|
NSURL *URL = [RCTConvert NSURL:options[@"url"]];
|
|
|
|
if (URL) {
|
|
|
|
if ([URL.scheme.lowercaseString isEqualToString:@"data"]) {
|
2016-02-22 13:33:31 +00:00
|
|
|
NSError *error;
|
2017-12-11 18:37:14 +00:00
|
|
|
NSData *data = [NSData dataWithContentsOfURL:URL
|
2016-02-22 13:33:31 +00:00
|
|
|
options:(NSDataReadingOptions)0
|
|
|
|
error:&error];
|
|
|
|
if (!data) {
|
|
|
|
failureCallback(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[items addObject:data];
|
|
|
|
} else {
|
2017-12-11 18:37:14 +00:00
|
|
|
[items addObject:URL];
|
2016-02-22 13:33:31 +00:00
|
|
|
}
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2017-12-11 18:37:14 +00:00
|
|
|
if (items.count == 0) {
|
|
|
|
RCTLogError(@"No `url` or `message` to share");
|
|
|
|
return;
|
|
|
|
}
|
2015-09-22 17:43:56 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
|
2015-11-19 16:40:47 +00:00
|
|
|
|
|
|
|
NSString *subject = [RCTConvert NSString:options[@"subject"]];
|
|
|
|
if (subject) {
|
|
|
|
[shareController setValue:subject forKey:@"subject"];
|
|
|
|
}
|
|
|
|
|
2015-11-30 14:18:55 +00:00
|
|
|
NSArray *excludedActivityTypes = [RCTConvert NSStringArray:options[@"excludedActivityTypes"]];
|
|
|
|
if (excludedActivityTypes) {
|
|
|
|
shareController.excludedActivityTypes = excludedActivityTypes;
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:03:34 +00:00
|
|
|
UIViewController *controller = RCTPresentedViewController();
|
2016-09-02 02:37:54 +00:00
|
|
|
shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
|
|
|
|
if (activityError) {
|
|
|
|
failureCallback(activityError);
|
|
|
|
} else {
|
2015-06-15 14:53:45 +00:00
|
|
|
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
|
2015-09-15 18:28:24 +00:00
|
|
|
}
|
2016-09-02 02:37:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
shareController.modalPresentationStyle = UIModalPresentationPopover;
|
|
|
|
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
|
|
|
|
if (!anchorViewTag) {
|
|
|
|
shareController.popoverPresentationController.permittedArrowDirections = 0;
|
2015-09-15 18:28:24 +00:00
|
|
|
}
|
2016-09-02 02:37:54 +00:00
|
|
|
shareController.popoverPresentationController.sourceView = controller.view;
|
|
|
|
shareController.popoverPresentationController.sourceRect = [self sourceRectInView:controller.view anchorViewTag:anchorViewTag];
|
2015-09-15 18:28:24 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
[controller presentViewController:shareController animated:YES completion:nil];
|
2015-12-09 13:08:36 +00:00
|
|
|
|
|
|
|
shareController.view.tintColor = [RCTConvert UIColor:options[@"tintColor"]];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|