A link can start your app from scratch or reach it while it is already running, and the two cases arrive through different APIs. Handle only one of them and some links will open the app without taking the user anywhere. The same Linking module also works in the other direction: it opens web pages, other apps and your app's own page in the system settings.

Key terms

  • Linking: the React Native module for both incoming and outgoing app links.
  • Universal Links: the iOS name for https deep links, standard https links configured to open an app.
  • Linking.getInitialURL(): returns a Promise for the URL that launched the app, or null when no link launched it.
  • LSApplicationQueriesSchemes: the Info.plist key listing the custom schemes your iOS app checks with canOpenURL.

An app can tell the operating system that it handles a custom scheme such as tripnote://, and it then receives the URL that opened it. Custom schemes have two weak spots. A link sent by email may be opened on a desktop computer, where tripnote:// does not work. And, as the PKCE lesson showed, nothing stops another app from registering the same scheme and receiving the data in your links.

Standard https links avoid the desktop problem: on mobile they can be configured to open your app. iOS calls these https deep links Universal Links, and they let an iOS app link to its own content securely. Whatever the scheme, keep tokens and other sensitive values out of links.

Each platform needs native setup before a link reaches JavaScript: intent filters on Android, and on iOS links forwarded to React Native from the AppDelegate, or from the SceneDelegate in apps that declare UIApplicationSceneManifest. The Linking page in the resources has the steps.

When the app is not running, the link launches it and the URL becomes the initial URL. Linking.getInitialURL() returns a Promise that resolves to that URL, or to null when the launch did not come from a link, for example from the home screen icon. Its type is Promise<null | undefined | string>, so check the value before using it.

When the app is already open, it comes to the foreground and Linking emits a url event. Linking.addEventListener('url', handler) calls the handler with an object holding the link, {url}, and returns an EventSubscription to remove in your cleanup. getInitialURL() only reports the link that launched the app, so an app needs both calls.

The useIncomingLinks hook below combines them. It passes the initial URL to onLink when there is one, then subscribes to url for every link after that. The listener destructures the event:

tsx
Linking.addEventListener('url', ({url}) => onLink(url))

A handler written as url => onLink(url) would pass the whole event object along.

Gaps in the native setup

Two setup details decide whether those calls ever see a link. On Android, receiving the link in the existing instance of MainActivity needs its launchMode set to singleTask in AndroidManifest.xml. On iOS with scenes, the SceneDelegate callbacks only cover links that arrive while the scene is connected. For a cold start you also hand the scene's UIScene.ConnectionOptions to React Native when you bootstrap it, or getInitialURL() resolves to null.

Sending users elsewhere

Linking.openURL(url) tries to open a URL with any installed app. https and http URLs open the browser, and mailto:, tel: and sms: open the mail, phone and SMS apps on both platforms. Web URLs need their protocol. The promise resolves when the user confirms the open dialog or the URL opens automatically, and rejects when the user cancels or no app is registered for the URL. An empty URL throws Invalid URL: cannot be empty before anything reaches native code. Simulators differ: the iOS simulator has no dialer, so a tel: link logs a warning and the promise resolves instead of rejecting.

For a URL that is not http(s), call Linking.canOpenURL(url) first. It returns Promise<boolean>. On iOS, list the scheme under LSApplicationQueriesSchemes. When a listed scheme has no installed app, the promise resolves false. When the scheme is missing from the list and no app can open the URL, the promise rejects with Unable to open URL: ... Add <scheme> to LSApplicationQueriesSchemes in your Info.plist. The Linking page also says the call resolves false in that case, so code that follows it and skips error handling gets an unhandled rejection. An http(s) URL that cannot be opened resolves false.

On Android, canOpenURL resolves with whether an installed activity can view the URL. When you target Android 11 (SDK 30), declare the intents for the schemes you query in AndroidManifest.xml.

The openEpisode example below tries a podcast app's custom scheme and falls back to the web page, treating a rejection like a false.

Linking.openSettings() opens the Settings app on your app's own settings. On Android that is the system's app details screen for your package, and on iOS the promise rejects with Unable to open app settings if it fails. It fits screens that ask the user to turn a declined permission back on.

Code Examples

tsx
import {useEffect} from 'react';
import {Linking} from 'react-native';

export function useIncomingLinks(onLink: (url: string) => void) {
  useEffect(() => {
    Linking.getInitialURL().then(url => {
      if (url) {
        onLink(url);
      }
    });

    const subscription = Linking.addEventListener('url', ({url}) =>
      onLink(url),
    );
    return () => subscription.remove();
  }, [onLink]);
}
tsx
import {Linking} from 'react-native';

export async function openEpisode(episodeId: string) {
  const appUrl = `podplayer://episodes/${episodeId}`;
  const webUrl = `https://podplayer.example.com/episodes/${episodeId}`;

  try {
    if (await Linking.canOpenURL(appUrl)) {
      await Linking.openURL(appUrl);
      return;
    }
  } catch {
    // iOS: podplayer is missing from LSApplicationQueriesSchemes
  }

  await Linking.openURL(webUrl);
}
✓ Completed