Skip to content

Commit

Permalink
Add support for Universal Links
Browse files Browse the repository at this point in the history
Summary:
Adds support for [Universal Links](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html).
Closes facebook#4109

Reviewed By: svcscm

Differential Revision: D2658105

Pulled By: nicklockwood

fb-gh-sync-id: 7d94564f64cda7d31c79cf8f4c450ed2387057be
  • Loading branch information
deminoth authored and facebook-github-bot-7 committed Dec 4, 2015
1 parent c71811e commit f4c286f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
21 changes: 17 additions & 4 deletions Libraries/LinkingIOS/LinkingIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,22 @@ var DEVICE_NOTIF_EVENT = 'openURL';
* execution you'll need to add the following lines to you `*AppDelegate.m`:
*
* ```
* - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
* return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
* - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
* sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
* {
* return [RCTLinkingManager application:application openURL:url
* sourceApplication:sourceApplication annotation:annotation];
* }
*
* // Only if your app is using [Universal Links](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html).
* - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity
* restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler
* {
* return [RCTLinkingManager application:application
* continueUserActivity:userActivity
* restorationHandler:restorationHandler];
* }
*
* ```
*
* And then on your React component you'll be able to listen to the events on
Expand All @@ -71,7 +84,7 @@ var DEVICE_NOTIF_EVENT = 'openURL';
* LinkingIOS.openURL(url)
* ```
*
* If you want to check if any installed app can handle a given URL beforehand you can call
* If you want to check if any installed app can handle a given URL beforehand, call
* ```
* LinkingIOS.canOpenURL(url, (supported) => {
* if (!supported) {
Expand Down Expand Up @@ -130,7 +143,7 @@ class LinkingIOS {
* Determine whether or not an installed app can handle a given URL.
* The callback function will be called with `bool supported` as the only argument
*
* NOTE: As of iOS 9, your app needs to provide a `LSApplicationQueriesSchemes` key
* NOTE: As of iOS 9, your app needs to provide the `LSApplicationQueriesSchemes` key
* inside `Info.plist`.
*/
static canOpenURL(url: string, callback: Function) {
Expand Down
4 changes: 4 additions & 0 deletions Libraries/LinkingIOS/RCTLinkingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;

+ (BOOL)application:(UIApplication *)application
continueUserActivity:(NSUserActivity *)userActivity
restorationHandler:(void (^)(NSArray *))restorationHandler;

@end
17 changes: 16 additions & 1 deletion Libraries/LinkingIOS/RCTLinkingManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ + (BOOL)application:(UIApplication *)application
return YES;
}

+ (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;
}

- (void)handleOpenURLNotification:(NSNotification *)notification
{
[_bridge.eventDispatcher sendDeviceEventWithName:@"openURL"
Expand All @@ -62,6 +75,7 @@ - (void)handleOpenURLNotification:(NSNotification *)notification

RCT_EXPORT_METHOD(openURL:(NSURL *)URL)
{
// TODO: we should really return success/failure via a callback here
// Doesn't really matter what thread we call this on since it exits the app
[RCTSharedApplication() openURL:URL];
}
Expand All @@ -72,7 +86,8 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
if (RCTRunningInAppExtension()) {
// Technically Today widgets can open urls, but supporting that would require
// a reference to the NSExtensionContext
callback(@[@(NO)]);
callback(@[@NO]);
return;
}

// This can be expensive, so we deliberately don't call on main thread
Expand Down

0 comments on commit f4c286f

Please sign in to comment.