From ff78a8de227d221a59507d0708dcd409fb3cc36b Mon Sep 17 00:00:00 2001 From: Jason Noah Choi Date: Thu, 25 May 2017 11:24:07 -0700 Subject: [PATCH] Add newly recommended method for RCTLinkingManager due to deprecation Summary: What existing problem does the pull request solve? Beginning in iOS9, Apple has deprecated `-application:openURL:sourceApplication:annotations:` `- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;` This PR uses the newly recommended method: `- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)` while meanwhile, leaving the deprecated one for developers wishing to use the older `-application:openURL:sourceApplication:annotations:` for apps that support versions 8.x or less. Benefits will include: - [x] less warnings - [x] official deprecation should happen when iOS 11 is deployed - [x] TVOS support Closes https://github.com/facebook/react-native/pull/13615 Differential Revision: D4987980 Pulled By: javache fbshipit-source-id: ae07715a55ca627860262a9c8cf7df1e3c5e752b --- Libraries/Linking/Linking.js | 23 ++++++++++------------- Libraries/LinkingIOS/RCTLinkingManager.h | 4 ++++ Libraries/LinkingIOS/RCTLinkingManager.m | 22 ++++++++++++++++++---- RNTester/RNTester/AppDelegate.m | 6 ++++++ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Libraries/Linking/Linking.js b/Libraries/Linking/Linking.js index 4962aae0409..98e10b7c45e 100644 --- a/Libraries/Linking/Linking.js +++ b/Libraries/Linking/Linking.js @@ -72,37 +72,35 @@ const LinkingManager = Platform.OS === 'android' ? * execution, you'll need to add the following lines to your `*AppDelegate.m`: * * ``` - * // iOS 10 + * // iOS 9.x or newer * #import + * * - (BOOL)application:(UIApplication *)application * openURL:(NSURL *)url * options:(NSDictionary *)options * { - * - * return [RCTLinkingManager application:application openURL:url - * sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] - * annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]; - * + * return [RCTLinkingManager application:app openURL:url options:options]; * } * ``` * - * If you're targeting iOS 9 or older, you can use the following code instead: + * If you're targeting iOS 8.x or older, you can use the following code instead: * * ``` - * // iOS 9 or older + * // iOS 8.x or older * #import * * - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url * sourceApplication:(NSString *)sourceApplication annotation:(id)annotation * { * return [RCTLinkingManager application:application openURL:url - * sourceApplication:sourceApplication annotation:annotation]; + * sourceApplication:sourceApplication annotation:annotation]; * } * ``` - * - * If your app is using [Universal Links](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html), + * + * + * // If your app is using [Universal Links](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html), * you'll need to add the following code as well: - * + * * ``` * - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity * restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler @@ -111,7 +109,6 @@ const LinkingManager = Platform.OS === 'android' ? * continueUserActivity:userActivity * restorationHandler:restorationHandler]; * } - * * ``` * * And then on your React component you'll be able to listen to the events on diff --git a/Libraries/LinkingIOS/RCTLinkingManager.h b/Libraries/LinkingIOS/RCTLinkingManager.h index b4bb4b5ce5e..8f5f8b89aa7 100644 --- a/Libraries/LinkingIOS/RCTLinkingManager.h +++ b/Libraries/LinkingIOS/RCTLinkingManager.h @@ -13,6 +13,10 @@ @interface RCTLinkingManager : RCTEventEmitter ++ (BOOL)application:(UIApplication *)app + openURL:(NSURL *)URL + options:(NSDictionary *)options; + + (BOOL)application:(UIApplication *)application openURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication diff --git a/Libraries/LinkingIOS/RCTLinkingManager.m b/Libraries/LinkingIOS/RCTLinkingManager.m index d5eb10be1fc..b156ab26a52 100644 --- a/Libraries/LinkingIOS/RCTLinkingManager.m +++ b/Libraries/LinkingIOS/RCTLinkingManager.m @@ -15,6 +15,15 @@ NSString *const RCTOpenURLNotification = @"RCTOpenURLNotification"; + +static void postNotificationWithURL(NSURL *URL, id sender) +{ + NSDictionary *payload = @{@"url": URL.absoluteString}; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTOpenURLNotification + object:sender + userInfo:payload]; +} + @implementation RCTLinkingManager RCT_EXPORT_MODULE() @@ -42,15 +51,20 @@ RCT_EXPORT_MODULE() return @[@"url"]; } ++ (BOOL)application:(UIApplication *)app + openURL:(NSURL *)URL + options:(NSDictionary *)options +{ + postNotificationWithURL(URL, self); + return YES; +} + + (BOOL)application:(UIApplication *)application openURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { - NSDictionary *payload = @{@"url": URL.absoluteString}; - [[NSNotificationCenter defaultCenter] postNotificationName:RCTOpenURLNotification - object:self - userInfo:payload]; + postNotificationWithURL(URL, self); return YES; } diff --git a/RNTester/RNTester/AppDelegate.m b/RNTester/RNTester/AppDelegate.m index b09747b5aae..55b9ab5f764 100644 --- a/RNTester/RNTester/AppDelegate.m +++ b/RNTester/RNTester/AppDelegate.m @@ -56,6 +56,12 @@ fallbackResource:nil]; } +- (BOOL)application:(UIApplication *)app + openURL:(NSURL *)url + options:(NSDictionary *)options +{ + return [RCTLinkingManager application:app openURL:url options:options]; +} - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation