RCTLinkingManager.mm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/RCTLinkingManager.h>
  8. #import <FBReactNativeSpec/FBReactNativeSpec.h>
  9. #import <React/RCTBridge.h>
  10. #import <React/RCTEventDispatcher.h>
  11. #import <React/RCTUtils.h>
  12. #import <React/RCTLog.h>
  13. #import "RCTLinkingPlugins.h"
  14. static NSString *const kOpenURLNotification = @"RCTOpenURLNotification";
  15. static void postNotificationWithURL(NSURL *URL, id sender)
  16. {
  17. NSDictionary<NSString *, id> *payload = @{@"url": URL.absoluteString};
  18. [[NSNotificationCenter defaultCenter] postNotificationName:kOpenURLNotification
  19. object:sender
  20. userInfo:payload];
  21. }
  22. @interface RCTLinkingManager() <NativeLinkingSpec>
  23. @end
  24. @implementation RCTLinkingManager
  25. RCT_EXPORT_MODULE()
  26. - (dispatch_queue_t)methodQueue
  27. {
  28. return dispatch_get_main_queue();
  29. }
  30. - (void)startObserving
  31. {
  32. [[NSNotificationCenter defaultCenter] addObserver:self
  33. selector:@selector(handleOpenURLNotification:)
  34. name:kOpenURLNotification
  35. object:nil];
  36. }
  37. - (void)stopObserving
  38. {
  39. [[NSNotificationCenter defaultCenter] removeObserver:self];
  40. }
  41. - (NSArray<NSString *> *)supportedEvents
  42. {
  43. return @[@"url"];
  44. }
  45. + (BOOL)application:(UIApplication *)app
  46. openURL:(NSURL *)URL
  47. options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
  48. {
  49. postNotificationWithURL(URL, self);
  50. return YES;
  51. }
  52. // Corresponding api deprecated in iOS 9
  53. + (BOOL)application:(UIApplication *)application
  54. openURL:(NSURL *)URL
  55. sourceApplication:(NSString *)sourceApplication
  56. annotation:(id)annotation
  57. {
  58. postNotificationWithURL(URL, self);
  59. return YES;
  60. }
  61. + (BOOL)application:(UIApplication *)application
  62. continueUserActivity:(NSUserActivity *)userActivity
  63. restorationHandler:
  64. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 12000) /* __IPHONE_12_0 */
  65. (nonnull void (^)(NSArray<id<UIUserActivityRestoring>> *_Nullable))restorationHandler {
  66. #else
  67. (nonnull void (^)(NSArray *_Nullable))restorationHandler {
  68. #endif
  69. if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
  70. NSDictionary *payload = @{@"url": userActivity.webpageURL.absoluteString};
  71. [[NSNotificationCenter defaultCenter] postNotificationName:kOpenURLNotification
  72. object:self
  73. userInfo:payload];
  74. }
  75. return YES;
  76. }
  77. - (void)handleOpenURLNotification:(NSNotification *)notification
  78. {
  79. [self sendEventWithName:@"url" body:notification.userInfo];
  80. }
  81. RCT_EXPORT_METHOD(openURL:(NSURL *)URL
  82. resolve:(RCTPromiseResolveBlock)resolve
  83. reject:(RCTPromiseRejectBlock)reject)
  84. {
  85. [RCTSharedApplication() openURL:URL options:@{} completionHandler:^(BOOL success) {
  86. if (success) {
  87. resolve(@YES);
  88. } else {
  89. #if TARGET_OS_SIMULATOR
  90. // Simulator-specific code
  91. if([URL.absoluteString hasPrefix:@"tel:"]){
  92. RCTLogWarn(@"Unable to open the Phone app in the simulator for telephone URLs. URL: %@", URL);
  93. resolve(@NO);
  94. } else {
  95. reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@", URL], nil);
  96. }
  97. #else
  98. // Device-specific code
  99. reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@", URL], nil);
  100. #endif
  101. }
  102. }];
  103. }
  104. RCT_EXPORT_METHOD(canOpenURL:(NSURL *)URL
  105. resolve:(RCTPromiseResolveBlock)resolve
  106. reject:(__unused RCTPromiseRejectBlock)reject)
  107. {
  108. if (RCTRunningInAppExtension()) {
  109. // Technically Today widgets can open urls, but supporting that would require
  110. // a reference to the NSExtensionContext
  111. resolve(@NO);
  112. return;
  113. }
  114. // This can be expensive, so we deliberately don't call on main thread
  115. BOOL canOpen = [RCTSharedApplication() canOpenURL:URL];
  116. NSString *scheme = [URL scheme];
  117. if (canOpen) {
  118. resolve(@YES);
  119. } else if (![[scheme lowercaseString] hasPrefix:@"http"] && ![[scheme lowercaseString] hasPrefix:@"https"]) {
  120. // On iOS 9 and above canOpenURL returns NO without a helpful error.
  121. // Check if a custom scheme is being used, and if it exists in LSApplicationQueriesSchemes
  122. NSArray *querySchemes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"LSApplicationQueriesSchemes"];
  123. if (querySchemes != nil && ([querySchemes containsObject:scheme] || [querySchemes containsObject:[scheme lowercaseString]])) {
  124. resolve(@NO);
  125. } else {
  126. reject(RCTErrorUnspecified, [NSString stringWithFormat:@"Unable to open URL: %@. Add %@ to LSApplicationQueriesSchemes in your Info.plist.", URL, scheme], nil);
  127. }
  128. } else {
  129. resolve(@NO);
  130. }
  131. }
  132. RCT_EXPORT_METHOD(getInitialURL:(RCTPromiseResolveBlock)resolve
  133. reject:(__unused RCTPromiseRejectBlock)reject)
  134. {
  135. NSURL *initialURL = nil;
  136. if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) {
  137. initialURL = self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
  138. } else {
  139. NSDictionary *userActivityDictionary =
  140. self.bridge.launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey];
  141. if ([userActivityDictionary[UIApplicationLaunchOptionsUserActivityTypeKey] isEqual:NSUserActivityTypeBrowsingWeb]) {
  142. initialURL = ((NSUserActivity *)userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"]).webpageURL;
  143. }
  144. }
  145. resolve(RCTNullIfNil(initialURL.absoluteString));
  146. }
  147. RCT_EXPORT_METHOD(openSettings:(RCTPromiseResolveBlock)resolve
  148. reject:(__unused RCTPromiseRejectBlock)reject)
  149. {
  150. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  151. [RCTSharedApplication() openURL:url options:@{} completionHandler:^(BOOL success) {
  152. if (success) {
  153. resolve(nil);
  154. } else {
  155. reject(RCTErrorUnspecified, @"Unable to open app settings", nil);
  156. }
  157. }];
  158. }
  159. RCT_EXPORT_METHOD(sendIntent:(NSString *)action
  160. extras:(NSArray *_Nullable)extras
  161. resolve:(RCTPromiseResolveBlock)resolve
  162. reject:(RCTPromiseRejectBlock)reject)
  163. {
  164. RCTLogError(@"Not implemented: %@", NSStringFromSelector(_cmd));
  165. }
  166. - (std::shared_ptr<facebook::react::TurboModule>)
  167. getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
  168. nativeInvoker:(std::shared_ptr<facebook::react::CallInvoker>)nativeInvoker
  169. perfLogger:(id<RCTTurboModulePerformanceLogger>)perfLogger
  170. {
  171. return std::make_shared<facebook::react::NativeLinkingSpecJSI>(self, jsInvoker, nativeInvoker, perfLogger);
  172. }
  173. @end
  174. Class RCTLinkingManagerCls(void) {
  175. return RCTLinkingManager.class;
  176. }