Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native Linking AddEventListener not working

Tags:

react-native

Hi I was trying to use React-Native's Linking library to listen to Linking changes and I followed the instructions on https://facebook.github.io/react-native/docs/linking.html. I can open external URL using openURL but Linking.addEventListener doesn't seem to work for me. I copied the code snippet:

componentDidMount() {    Linking.addEventListener('url', this._handleOpenURL);  },  componentWillUnmount() {    Linking.removeEventListener('url', this._handleOpenURL);  },  _handleOpenURL(event) {    console.log(event.url);  }

it doesn't give me an error but the _handleOpenURL is not called when the app opens up a external URL.

I wonder why is this case and what should I do to fix it?

like image 903
Danqi Liao Avatar asked Oct 08 '16 20:10

Danqi Liao


People also ask

What is linking addEventListener?

addEventListener() ​Add a handler to Linking changes by listening to the url event type and providing the handler.

How do I link a package in react native?

1) Go to your project's home dir using cmd. 2) run npm install 3) Thereafter run rnpm link or react-native link 4) see ios folder in your project folder and if you find any pod file then run pod install after navigating into ios folder in cmd.

How do you implement deep linking in react native?

If you want to add it manually, open up SimpleApp/android/app/src/main/AndroidManifest. xml , and make the following adjustments: Set launchMode of MainActivity to singleTask in order to receive intent on existing MainActivity (this is the default, so you may not need to actually change anything).


2 Answers

This is because Linking have a specific method when the app start through a intent.

Try with this:

componentDidMount() {   Linking.getInitialURL().then((ev) => {     if (ev) {       this._handleOpenURL(ev);     }   }).catch(err => {       console.warn('An error occurred', err);   });   Linking.addEventListener('url', this._handleOpenURL); } 
like image 160
Carlos Quiroga Avatar answered Oct 21 '22 04:10

Carlos Quiroga


I had the same problem that I managed to fix for 2 days. Here are my steps, I hope it will help to the next one that needs to manage to fix this problem.

  1. Go to the React Native Documentation for your version (IMPORTANT) - https://facebook.github.io/react-native/versions
  2. Go to Linking API documentation (Follow the steps)

In my case, I just added this method

// iOS 9.x or newer #import <React/RCTLinkingManager.h>  - (BOOL)application:(UIApplication *)application    openURL:(NSURL *)url    options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {   return [RCTLinkingManager application:application openURL:url options:options]; } 

After that, the event listener will work properly. Check the IOS version there is a snippet for 8.x and bellow.

like image 22
Ivan Abadzhiev Avatar answered Oct 21 '22 05:10

Ivan Abadzhiev