2015-03-26 01:59: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTLinkingManager.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-06-12 18:05:01 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-03-26 01:59:42 +00:00
|
|
|
|
|
|
|
NSString *const RCTOpenURLNotification = @"RCTOpenURLNotification";
|
|
|
|
|
|
|
|
@implementation RCTLinkingManager
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
- (void)startObserving
|
2015-03-26 01:59:42 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleOpenURLNotification:)
|
|
|
|
name:RCTOpenURLNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
- (void)stopObserving
|
2015-11-25 11:09:00 +00:00
|
|
|
{
|
2016-05-27 17:14:12 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
2015-03-26 01:59:42 +00:00
|
|
|
{
|
2016-05-27 17:14:12 +00:00
|
|
|
return @[@"url"];
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL)application:(UIApplication *)application
|
2015-04-10 00:34:21 +00:00
|
|
|
openURL:(NSURL *)URL
|
2015-03-26 01:59:42 +00:00
|
|
|
sourceApplication:(NSString *)sourceApplication
|
|
|
|
annotation:(id)annotation
|
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
NSDictionary<NSString *, id> *payload = @{@"url": URL.absoluteString};
|
2015-03-26 01:59:42 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTOpenURLNotification
|
|
|
|
object:self
|
|
|
|
userInfo:payload];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-12-04 16:05:11 +00:00
|
|
|
+ (BOOL)application:(UIApplication *)application
|
|
|
|
continueUserActivity:(NSUserActivity *)userActivity
|
|
|
|
restorationHandler:(void (^)(NSArray *))restorationHandler
|
|
|
|
{
|
|
|
|
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
|
|
|
|
NSDictionary *payload = @{@"url": userActivity.webpageURL.absoluteString};
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTOpenURLNotification
|
|
|
|
object:self
|
|
|
|
userInfo:payload];
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
- (void)handleOpenURLNotification:(NSNotification *)notification
|
|
|
|
{
|
2016-05-27 17:14:12 +00:00
|
|
|
[self sendEventWithName:@"url" body:notification.userInfo];
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:34:00 +00:00
|
|
|
RCT_EXPORT_METHOD(openURL:(NSURL *)URL
|
|
|
|
resolve:(RCTPromiseResolveBlock)resolve
|
2016-05-27 17:14:12 +00:00
|
|
|
reject:(RCTPromiseRejectBlock)reject)
|
2015-03-26 01:59:42 +00:00
|
|
|
{
|
2016-02-10 17:25:30 +00:00
|
|
|
BOOL opened = [RCTSharedApplication() openURL:URL];
|
2016-05-27 17:14:12 +00:00
|
|
|
if (opened) {
|
|
|
|
resolve(nil);
|
|
|
|
} else {
|
|
|
|
reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@", URL], nil);
|
|
|
|
}
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 00:34:21 +00:00
|
|
|
RCT_EXPORT_METHOD(canOpenURL:(NSURL *)URL
|
2016-01-26 22:34:00 +00:00
|
|
|
resolve:(RCTPromiseResolveBlock)resolve
|
|
|
|
reject:(__unused RCTPromiseRejectBlock)reject)
|
2015-03-26 01:59:42 +00:00
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
// Technically Today widgets can open urls, but supporting that would require
|
|
|
|
// a reference to the NSExtensionContext
|
2016-02-10 15:24:38 +00:00
|
|
|
resolve(@NO);
|
2015-12-04 16:05:11 +00:00
|
|
|
return;
|
2015-09-22 17:43:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
// TODO: on iOS9 this will fail if URL isn't included in the plist
|
|
|
|
// we should probably check for that and reject in that case instead of
|
|
|
|
// simply resolving with NO
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
// This can be expensive, so we deliberately don't call on main thread
|
2015-09-22 17:43:56 +00:00
|
|
|
BOOL canOpen = [RCTSharedApplication() canOpenURL:URL];
|
2016-02-10 15:24:38 +00:00
|
|
|
resolve(@(canOpen));
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:14:12 +00:00
|
|
|
RCT_EXPORT_METHOD(getInitialURL:(RCTPromiseResolveBlock)resolve
|
|
|
|
reject:(__unused RCTPromiseRejectBlock)reject)
|
|
|
|
{
|
|
|
|
NSURL *initialURL = nil;
|
|
|
|
if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
|
|
|
|
initialURL = self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
|
2016-09-02 02:37:54 +00:00
|
|
|
} else {
|
2016-05-27 17:14:12 +00:00
|
|
|
NSDictionary *userActivityDictionary =
|
|
|
|
self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
|
|
|
|
if ([userActivityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey] isEqual:NSUserActivityTypeBrowsingWeb]) {
|
|
|
|
initialURL = ((NSUserActivity *)userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"]).webpageURL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolve(RCTNullIfNil(initialURL.absoluteString));
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
@end
|