Skip to content

Commit

Permalink
Updated Linking and PushNotificationIOS modules to use NativeEventEmi…
Browse files Browse the repository at this point in the history
…tter

Reviewed By: javache

Differential Revision: D3352819

fbshipit-source-id: d218791a16aba597d2544691ef993711cf00522c
  • Loading branch information
nicklockwood authored and Facebook Github Bot 7 committed May 27, 2016
1 parent a4b5f1b commit 1623e34
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 302 deletions.
71 changes: 25 additions & 46 deletions Libraries/Linking/Linking.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@
*/
'use strict';

const NativeEventEmitter = require('NativeEventEmitter');
const NativeModules = require('NativeModules');
const Platform = require('Platform');
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
const {
IntentAndroid,
LinkingManager: LinkingManagerIOS
} = require('NativeModules');
const LinkingManager = Platform.OS === 'android' ? IntentAndroid : LinkingManagerIOS;
const invariant = require('fbjs/lib/invariant');
const Map = require('Map');

const _notifHandlers = new Map();
const invariant = require('fbjs/lib/invariant');

const DEVICE_NOTIF_EVENT = 'openURL';
const LinkingManager = Platform.OS === 'android' ?
NativeModules.IntentAndroid : NativeModules.LinkingManager;

/**
* `Linking` gives you a general interface to interact with both incoming
Expand Down Expand Up @@ -110,26 +105,23 @@ const DEVICE_NOTIF_EVENT = 'openURL';
* }).catch(err => console.error('An error occurred', err));
* ```
*/
class Linking {
class Linking extends NativeEventEmitter {

constructor() {
super(LinkingManager);
}

/**
* Add a handler to Linking changes by listening to the `url` event type
* and providing the handler
*
* @platform ios
*/
static addEventListener(type: string, handler: Function) {
addEventListener(type: string, handler: Function) {
if (Platform.OS === 'android') {
console.warn('Linking.addEventListener is not supported on Android');
console.warn('Linking.addEventListener is not supported on Android');
} else {
invariant(
type === 'url',
'Linking only supports `url` events'
);
var listener = RCTDeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
handler
);
_notifHandlers.set(handler, listener);
this.addListener(type, handler);
}
}

Expand All @@ -138,20 +130,11 @@ class Linking {
*
* @platform ios
*/
static removeEventListener(type: string, handler: Function ) {
removeEventListener(type: string, handler: Function ) {
if (Platform.OS === 'android') {
console.warn('Linking.removeEventListener is not supported on Android');
console.warn('Linking.removeEventListener is not supported on Android');
} else {
invariant(
type === 'url',
'Linking only supports `url` events'
);
var listener = _notifHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
_notifHandlers.delete(handler);
this.removeListener(type, handler);
}
}

Expand All @@ -166,7 +149,7 @@ class Linking {
*
* NOTE: For web URLs, the protocol ("http://", "https://") must be set accordingly!
*/
static openURL(url: string): Promise<boolean> {
openURL(url: string): Promise {
this._validateURL(url);
return LinkingManager.openURL(url);
}
Expand All @@ -177,30 +160,26 @@ class Linking {
* NOTE: For web URLs, the protocol ("http://", "https://") must be set accordingly!
*
* NOTE: As of iOS 9, your app needs to provide the `LSApplicationQueriesSchemes` key
* inside `Info.plist`.
* inside `Info.plist` or canOpenURL will always return false.
*
* @param URL the URL to open
*/
static canOpenURL(url: string): Promise<boolean> {
canOpenURL(url: string): Promise<boolean> {
this._validateURL(url);
return LinkingManager.canOpenURL(url);
}

/**
* If the app launch was triggered by an app link with,
* If the app launch was triggered by an app link,
* it will give the link url, otherwise it will give `null`
*
* NOTE: To support deep linking on Android, refer http://developer.android.com/training/app-indexing/deep-linking.html#handling-intents
*/
static getInitialURL(): Promise<?string> {
if (Platform.OS === 'android') {
return IntentAndroid.getInitialURL();
} else {
return Promise.resolve(LinkingManagerIOS.initialURL);
}
getInitialURL(): Promise<?string> {
return LinkingManager.getInitialURL();
}

static _validateURL(url: string) {
_validateURL(url: string) {
invariant(
typeof url === 'string',
'Invalid URL: should be a string. Was: ' + url
Expand All @@ -212,4 +191,4 @@ class Linking {
}
}

module.exports = Linking;
module.exports = new Linking();
145 changes: 2 additions & 143 deletions Libraries/LinkingIOS/LinkingIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,148 +12,7 @@
'use strict';

var Linking = require('Linking');
var RCTLinkingManager = require('NativeModules').LinkingManager;
var invariant = require('fbjs/lib/invariant');

var _initialURL = RCTLinkingManager && RCTLinkingManager.initialURL;
console.warn('LinkingIOS is deprecated. Use Linking instead');

/**
* NOTE: `LinkingIOS` is being deprecated. Use `Linking` instead.
*
* `LinkingIOS` gives you a general interface to interact with both incoming
* and outgoing app links.
*
* ### Basic Usage
*
* #### Handling deep links
*
* If your app was launched from an external url registered to your app you can
* access and handle it from any component you want with
*
* ```
* componentDidMount() {
* var url = LinkingIOS.popInitialURL();
* }
* ```
*
* In case you also want to listen to incoming app links during your app's
* 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];
* }
*
* // 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
* `LinkingIOS` as follows
*
* ```
* componentDidMount() {
* LinkingIOS.addEventListener('url', this._handleOpenURL);
* },
* componentWillUnmount() {
* LinkingIOS.removeEventListener('url', this._handleOpenURL);
* },
* _handleOpenURL(event) {
* console.log(event.url);
* }
* ```
*
* #### Triggering App links
*
* To trigger an app link (browser, email or custom schemas), call
*
* ```
* LinkingIOS.openURL(url)
* ```
*
* If you want to check if any installed app can handle a given URL beforehand, call
* ```
* LinkingIOS.canOpenURL(url, (supported) => {
* if (!supported) {
* AlertIOS.alert('Can\'t handle url: ' + url);
* } else {
* LinkingIOS.openURL(url);
* }
* });
* ```
*/
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) {
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 ) {
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) {
console.warn('"LinkingIOS.openURL" is deprecated. Use the promise based "Linking.openURL" instead.');
Linking.openURL(url);
}

/**
* 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 the `LSApplicationQueriesSchemes` key
* inside `Info.plist`.
*
* @deprecated
*/
static canOpenURL(url: string, callback: Function) {
console.warn('"LinkingIOS.canOpenURL" is deprecated. Use the promise based "Linking.canOpenURL" instead.');
invariant(
typeof callback === 'function',
'A valid callback function is required'
);
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;
}
}

module.exports = LinkingIOS;
module.exports = Linking;
4 changes: 2 additions & 2 deletions Libraries/LinkingIOS/RCTLinkingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

#import <UIKit/UIKit.h>

#import "RCTBridgeModule.h"
#import "RCTEventEmitter.h"

@interface RCTLinkingManager : NSObject <RCTBridgeModule>
@interface RCTLinkingManager : RCTEventEmitter

+ (BOOL)application:(UIApplication *)application
openURL:(NSURL *)URL
Expand Down
Loading

0 comments on commit 1623e34

Please sign in to comment.