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-03-13 17:45:27 +00:00
|
|
|
NSMutableDictionary *_callbacks;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-13 17:45:27 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_callbacks = [NSMutableDictionary new];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(showActionSheetWithOptions:(NSDictionary *)options
|
2015-06-15 14:53:45 +00:00
|
|
|
failureCallback:(__unused RCTResponseSenderBlock)failureCallback
|
2015-04-08 15:52:48 +00:00
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback)
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
UIActionSheet *actionSheet = [UIActionSheet new];
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
actionSheet.title = options[@"title"];
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
for (NSString *option in options[@"options"]) {
|
|
|
|
[actionSheet addButtonWithTitle:option];
|
|
|
|
}
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
if (options[@"destructiveButtonIndex"]) {
|
|
|
|
actionSheet.destructiveButtonIndex = [options[@"destructiveButtonIndex"] integerValue];
|
|
|
|
}
|
|
|
|
if (options[@"cancelButtonIndex"]) {
|
|
|
|
actionSheet.cancelButtonIndex = [options[@"cancelButtonIndex"] integerValue];
|
|
|
|
}
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
actionSheet.delegate = self;
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
_callbacks[RCTKeyForInstance(actionSheet)] = successCallback;
|
2015-03-13 17:45:27 +00:00
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
UIWindow *appWindow = [UIApplication sharedApplication].delegate.window;
|
2015-04-18 17:43:20 +00:00
|
|
|
if (appWindow == nil) {
|
|
|
|
RCTLogError(@"Tried to display action sheet but there is no application window. options: %@", options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[actionSheet showInView:appWindow];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(NSDictionary *)options
|
|
|
|
failureCallback:(RCTResponseSenderBlock)failureCallback
|
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback)
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
NSMutableArray *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) {
|
|
|
|
[items addObject:URL];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-08-24 10:14:33 +00:00
|
|
|
if (items.count == 0) {
|
2015-04-18 17:43:20 +00:00
|
|
|
failureCallback(@[@"No `url` or `message` to share"]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UIActivityViewController *share = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
|
2015-08-24 10:14:33 +00:00
|
|
|
UIViewController *ctrl = [UIApplication sharedApplication].delegate.window.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
|
|
|
|
share.completionHandler = ^(NSString *activityType, BOOL completed) {
|
|
|
|
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
|
|
|
|
share.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) {
|
|
|
|
if (activityError) {
|
|
|
|
failureCallback(@[RCTNullIfNil(activityError.localizedDescription)]);
|
|
|
|
} else {
|
2015-06-12 18:05:01 +00:00
|
|
|
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
|
2015-06-15 14:53:45 +00:00
|
|
|
}
|
|
|
|
};
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-09-15 18:28:24 +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.
|
|
|
|
*/
|
|
|
|
if ([share respondsToSelector:@selector(popoverPresentationController)]) {
|
|
|
|
share.popoverPresentationController.sourceView = ctrl.view;
|
|
|
|
NSNumber *anchorViewTag = [RCTConvert NSNumber:options[@"anchor"]];
|
|
|
|
if (anchorViewTag) {
|
|
|
|
UIView *anchorView = [self.bridge.uiManager viewForReactTag:anchorViewTag];
|
|
|
|
share.popoverPresentationController.sourceRect = [anchorView convertRect:anchorView.bounds toView:ctrl.view];
|
|
|
|
} else {
|
|
|
|
CGRect sourceRect = CGRectMake(ctrl.view.center.x, ctrl.view.center.y, 1, 1);
|
|
|
|
share.popoverPresentationController.sourceRect = sourceRect;
|
|
|
|
share.popoverPresentationController.permittedArrowDirections = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
[ctrl presentViewController:share animated:YES completion:nil];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark UIActionSheetDelegate Methods
|
|
|
|
|
|
|
|
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
|
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
NSString *key = RCTKeyForInstance(actionSheet);
|
2015-03-13 17:45:27 +00:00
|
|
|
RCTResponseSenderBlock callback = _callbacks[key];
|
|
|
|
if (callback) {
|
|
|
|
callback(@[@(buttonIndex)]);
|
|
|
|
[_callbacks removeObjectForKey:key];
|
|
|
|
} else {
|
|
|
|
RCTLogWarn(@"No callback registered for action sheet: %@", actionSheet.title);
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
[[UIApplication sharedApplication].delegate.window makeKeyWindow];
|
2015-03-13 17:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Private
|
|
|
|
|
2015-04-18 17:43:20 +00:00
|
|
|
static NSString *RCTKeyForInstance(id instance)
|
2015-03-13 17:45:27 +00:00
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"%p", instance];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|