question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Linking.addEventListener not working: url event never fires

See original GitHub issue

Description

Url event never fires

I followed the indicated setup to handle deep links in react native.

But when I have the app open in background or foreground and I execute command npx uri-scheme open "mychat://bar" --ios, url event doesn’t fire.

I saw there is a similar issue in react-navigation, but the fix indicated for AppDelegate.m doesn’t work for me.

Version

0.76

Output of npx react-native info

System: OS: macOS 12.5.1 CPU: (16) x64 Intel® Core™ i9-9880H CPU @ 2.30GHz Memory: 1.30 GB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.18.1 - /usr/local/opt/node@16/bin/node Yarn: 1.22.11 - /usr/local/bin/yarn npm: 8.19.2 - /usr/local/opt/node@16/bin/npm Watchman: Not Found Managers: CocoaPods: 1.11.2 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 16.0, macOS 12.3, tvOS 16.0, watchOS 9.0 Android SDK: API Levels: 28, 31, 33 Build Tools: 30.0.2, 31.0.0, 33.0.0 System Images: android-31 | Google APIs Intel x86 Atom_64, android-31 | Google Play Intel x86 Atom_64 Android NDK: Not Found IDEs: Android Studio: 2021.2 AI-212.5712.43.2112.8815526 Xcode: 14.0.1/14A400 - /usr/bin/xcodebuild Languages: Java: 13.0.2 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.1.0 => 18.1.0 react-native: 0.70.6 => 0.70.6 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

clone repo https://github.com/leoparis89/deeplink

  1. yarn
  2. cd ios && pod install
  3. yarn ios
  4. put app in background
  5. run command npx uri-scheme open "mychat://bar" --ios. App gets focused but url event never fires in ./App.js line 121.

Snack, code example, screenshot, or link to a repository

https://github.com/leoparis89/deeplink

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12

github_iconTop GitHub Comments

3reactions
edwardhsuehcommented, Sep 23, 2022

Yes, I do follow the document but still fail. I can’t run in simulator, so I use browser with button to call the url scheme. if app is closed, it works fine. But if app is in background, the url scheme called just bring app to foreground, but the url listener is not fired. My RN is 0.68.2, older than yours. Will this be a problem? By the way, can your app receive url event when it is in background?

2reactions
vickfancommented, Oct 6, 2022

Hi @edwardhsueh, I was using react native 0.68.0 and facing the same problem One thing that you can try is adding the below code to the location that specified by React Navigation Documentation as I am not familiar with Swift and placing the code in the wrong location

// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>

// Add this inside `@implementation AppDelegate` above `@end`:
- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

It’s working for me now

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Native Linking AddEventListener not working
I'm using bare workflow and this is how I implement Linking event listener to my ... addEventListener('url', handleDeepLink); Linking.
Read more >
Linking is working only if app is not running before opening ...
If the app is already open in the background it gets focussed, but the event listener never gets fired. Here's the code I'm...
Read more >
Deep linking - React Navigation
We want paths like chat/Eric to link to a "Chat" screen with the user passed as a param. Let's re-configure our chat screen...
Read more >
Introduction to events - Learn web development | MDN
Objects that can fire events have an addEventListener() method, that takes at least two arguments: the name of the event and a function...
Read more >
Expo deeplinking issue with addEventListener
However the addEventListener call is not being called at all when the app ... in the linking page so the event listener should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found