Add cross-platform Linking module

Summary:
A promise based API for handling Link for Android and iOS. Refer #4971

The iOS part doesn't handle errors. Will need someone with iOS knowledge to do that.

cc skevy ide brentvatne mkonicek vjeux nicklockwood
Closes https://github.com/facebook/react-native/pull/5336

Reviewed By: svcscm

Differential Revision: D2866664

Pulled By: androidtrunkagent

fb-gh-sync-id: 67e68a827e6b85886bfa84e79b897f079e78b1b5
This commit is contained in:
Satyajit Sahoo
2016-01-26 14:34:00 -08:00
committed by facebook-github-bot-5
parent affd6230fe
commit e33e6ab1f0
7 changed files with 281 additions and 73 deletions

View File

@@ -11,17 +11,15 @@
*/
'use strict';
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var Linking = require('Linking');
var RCTLinkingManager = require('NativeModules').LinkingManager;
var invariant = require('invariant');
var _notifHandlers = new Map();
var _initialURL = RCTLinkingManager &&
RCTLinkingManager.initialURL;
var DEVICE_NOTIF_EVENT = 'openURL';
var _initialURL = RCTLinkingManager.initialURL;
/**
* NOTE: `LinkingIOS` is being deprecated. Use `Linking` instead.
*
* `LinkingIOS` gives you a general interface to interact with both incoming
* and outgoing app links.
*
@@ -98,44 +96,32 @@ class LinkingIOS {
/**
* Add a handler to LinkingIOS changes by listening to the `url` event type
* and providing the handler
*
* @deprecated
*/
static addEventListener(type: string, handler: Function) {
invariant(
type === 'url',
'LinkingIOS only supports `url` events'
);
var listener = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
handler
);
_notifHandlers.set(handler, listener);
console.warn('"LinkingIOS.addEventListener" is deprecated. Use "Linking.addEventListener" instead.');
Linking.addEventListener(type, handler);
}
/**
* Remove a handler by passing the `url` event type and the handler
*
* @deprecated
*/
static removeEventListener(type: string, handler: Function ) {
invariant(
type === 'url',
'LinkingIOS only supports `url` events'
);
var listener = _notifHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
_notifHandlers.delete(handler);
console.warn('"LinkingIOS.removeEventListener" is deprecated. Use "Linking.removeEventListener" instead.');
Linking.removeEventListener(type, handler);
}
/**
* Try to open the given `url` with any of the installed apps.
*
* @deprecated
*/
static openURL(url: string) {
invariant(
typeof url === 'string',
'Invalid url: should be a string'
);
RCTLinkingManager.openURL(url);
console.warn('"LinkingIOS.openURL" is deprecated. Use the promise based "Linking.openURL" instead.');
Linking.openURL(url);
}
/**
@@ -144,24 +130,26 @@ class LinkingIOS {
*
* NOTE: As of iOS 9, your app needs to provide the `LSApplicationQueriesSchemes` key
* inside `Info.plist`.
*
* @deprecated
*/
static canOpenURL(url: string, callback: Function) {
invariant(
typeof url === 'string',
'Invalid url: should be a string'
);
console.warn('"LinkingIOS.canOpenURL" is deprecated. Use the promise based "Linking.canOpenURL" instead.');
invariant(
typeof callback === 'function',
'A valid callback function is required'
);
RCTLinkingManager.canOpenURL(url, callback);
Linking.canOpenURL(url).then(callback);
}
/**
* If the app launch was triggered by an app link, it will pop the link url,
* otherwise it will return `null`
*
* @deprecated
*/
static popInitialURL(): ?string {
console.warn('"LinkingIOS.popInitialURL" is deprecated. Use the promise based "Linking.getInitialURL" instead.');
var initialURL = _initialURL;
_initialURL = null;
return initialURL;

View File

@@ -73,26 +73,30 @@ continueUserActivity:(NSUserActivity *)userActivity
body:notification.userInfo];
}
RCT_EXPORT_METHOD(openURL:(NSURL *)URL)
RCT_EXPORT_METHOD(openURL:(NSURL *)URL
resolve:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
// TODO: we should really return success/failure via a callback here
// TODO: we should really report success/failure via the promise here
// Doesn't really matter what thread we call this on since it exits the app
[RCTSharedApplication() openURL:URL];
resolve(@[@YES]);
}
RCT_EXPORT_METHOD(canOpenURL:(NSURL *)URL
callback:(RCTResponseSenderBlock)callback)
resolve:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject)
{
if (RCTRunningInAppExtension()) {
// Technically Today widgets can open urls, but supporting that would require
// a reference to the NSExtensionContext
callback(@[@NO]);
resolve(@[@NO]);
return;
}
// This can be expensive, so we deliberately don't call on main thread
BOOL canOpen = [RCTSharedApplication() canOpenURL:URL];
callback(@[@(canOpen)]);
resolve(@[@(canOpen)]);
}
@end