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?
addEventListener() Add a handler to Linking changes by listening to the url event type and providing the handler.
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.
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).
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); }
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With