2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-13 17:45:27 +00:00
|
|
|
|
|
|
|
#import "RCTActionSheetManager.h"
|
|
|
|
|
2015-08-04 23:18:44 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-03-13 17:45:27 +00:00
|
|
|
#import "RCTLog.h"
|
2015-06-12 18:05:01 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-09-15 18:28:24 +00:00
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTUIManager.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()
|
|
|
|
|
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;
|
|
|
|
|
2015-11-05 11:49:41 +00:00
|
|
|
UIViewController *controller = RCTKeyWindow().rootViewController;
|
2016-01-12 11:13:21 +00:00
|
|
|
while (controller.presentedViewController) {
|
|
|
|
controller = controller.presentedViewController;
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
if ([UIAlertController class] == nil) {
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
UIActionSheet *actionSheet = [UIActionSheet new];
|
|
|
|
|
|
|
|
actionSheet.title = title;
|
|
|
|
for (NSString *option in buttons) {
|
|
|
|
[actionSheet addButtonWithTitle:option];
|
|
|
|
}
|
|
|
|
actionSheet.destructiveButtonIndex = destructiveButtonIndex;
|
|
|
|
actionSheet.cancelButtonIndex = cancelButtonIndex;
|
|
|
|
actionSheet.delegate = self;
|
|
|
|
|
|
|
|
[_callbacks setObject:callback forKey:actionSheet];
|
|
|
|
|
|
|
|
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
[actionSheet showFromRect:sourceRect inView:sourceView animated:YES];
|
|
|
|
} else {
|
|
|
|
[actionSheet showInView:sourceView];
|
|
|
|
}
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
UIAlertController *alertController =
|
|
|
|
[UIAlertController alertControllerWithTitle:title
|
2016-03-31 05:10:00 +00:00
|
|
|
message:message
|
2015-10-21 19:19:33 +00:00
|
|
|
preferredStyle:UIAlertControllerStyleActionSheet];
|
|
|
|
|
|
|
|
NSInteger index = 0;
|
|
|
|
for (NSString *option in buttons) {
|
|
|
|
UIAlertActionStyle style = UIAlertActionStyleDefault;
|
|
|
|
if (index == destructiveButtonIndex) {
|
|
|
|
style = UIAlertActionStyleDestructive;
|
|
|
|
} else if (index == cancelButtonIndex) {
|
|
|
|
style = UIAlertActionStyleCancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSInteger localIndex = index;
|
|
|
|
[alertController addAction:[UIAlertAction actionWithTitle:option
|
|
|
|
style:style
|
|
|
|
handler:^(__unused UIAlertAction *action){
|
|
|
|
callback(@[@(localIndex)]);
|
|
|
|
}]];
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
alertController.modalPresentationStyle = UIModalPresentationPopover;
|
|
|
|
alertController.popoverPresentationController.sourceView = sourceView;
|
|
|
|
alertController.popoverPresentationController.sourceRect = sourceRect;
|
|
|
|
if (!anchorViewTag) {
|
|
|
|
alertController.popoverPresentationController.permittedArrowDirections = 0;
|
|
|
|
}
|
|
|
|
[controller presentViewController:alertController animated:YES completion:nil];
|
2015-12-09 13:08:36 +00:00
|
|
|
|
|
|
|
alertController.view.tintColor = [RCTConvert UIColor:options[@"tintColor"]];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
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];
|
|
|
|
}
|
2015-08-04 23:18:44 +00:00
|
|
|
NSURL *URL = [RCTConvert NSURL:options[@"url"]];
|
|
|
|
if (URL) {
|
2016-02-22 13:33:31 +00:00
|
|
|
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];
|
|
|
|
}
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-08-24 10:14:33 +00:00
|
|
|
if (items.count == 0) {
|
2015-10-21 19:19:33 +00:00
|
|
|
RCTLogError(@"No `url` or `message` to share");
|
2015-09-22 17:43:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-05 11:49:41 +00:00
|
|
|
UIViewController *controller = RCTKeyWindow().rootViewController;
|
2015-03-26 04:29:28 +00:00
|
|
|
|
|
|
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
if (![UIActivityViewController instancesRespondToSelector:@selector(setCompletionWithItemsHandler:)]) {
|
|
|
|
// Legacy iOS 7 implementation
|
2015-10-21 19:19:33 +00:00
|
|
|
shareController.completionHandler = ^(NSString *activityType, BOOL completed) {
|
2015-06-15 14:53:45 +00:00
|
|
|
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
|
|
|
|
};
|
|
|
|
} else
|
2015-03-26 04:29:28 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
{
|
|
|
|
// iOS 8 version
|
2015-10-21 19:19:33 +00:00
|
|
|
shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
|
2015-06-15 14:53:45 +00:00
|
|
|
if (activityError) {
|
2015-10-21 19:19:33 +00:00
|
|
|
failureCallback(activityError);
|
2015-06-15 14:53:45 +00:00
|
|
|
} else {
|
2015-06-12 18:05:01 +00:00
|
|
|
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
|
2015-06-15 14:53:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-21 19:19:33 +00:00
|
|
|
shareController.modalPresentationStyle = UIModalPresentationPopover;
|
2015-09-15 18:28:24 +00:00
|
|
|
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
|
2015-10-21 19:19:33 +00:00
|
|
|
if (!anchorViewTag) {
|
|
|
|
shareController.popoverPresentationController.permittedArrowDirections = 0;
|
2015-09-15 18:28:24 +00:00
|
|
|
}
|
2015-10-21 19:19:33 +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
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark UIActionSheetDelegate Methods
|
|
|
|
|
|
|
|
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
|
|
|
|
{
|
2015-10-21 19:19:33 +00:00
|
|
|
RCTResponseSenderBlock callback = [_callbacks objectForKey:actionSheet];
|
2015-03-13 17:45:27 +00:00
|
|
|
if (callback) {
|
|
|
|
callback(@[@(buttonIndex)]);
|
2015-10-21 19:19:33 +00:00
|
|
|
[_callbacks removeObjectForKey:actionSheet];
|
2015-03-13 17:45:27 +00:00
|
|
|
} else {
|
|
|
|
RCTLogWarn(@"No callback registered for action sheet: %@", actionSheet.title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|