Skip to content

Instantly share code, notes, and snippets.

@diachedelic
Last active October 31, 2024 07:31
Show Gist options
  • Save diachedelic/0d60233dab3dcae3215da8a4dfdcd434 to your computer and use it in GitHub Desktop.
Save diachedelic/0d60233dab3dcae3215da8a4dfdcd434 to your computer and use it in GitHub Desktop.
Deep link to a native app from a browser, with a fallback
@vatsal-gadhiya-searce
Copy link

So Universal links only :) No solution.

@biandtlp
Copy link

Hello, is it possible to hide the alert "Safari cannot open the page because the address is invalid"?
Or we have to use Universal links for this?
Thank you

@diachedelic
Copy link
Author

It is not possible "check" whether the browser can handle a certain type of deep link for security reasons, sorry @biandtlp.

@luizcieslak
Copy link

Hey @diachedelic, very nice writeup. Do you happen to know if there's a way to open to Safari via deeplink? I tested using this URL for google chrome: googlechrome://www.google.com and it works nicely but I wonder if there's the same thing for Safari.

@diachedelic
Copy link
Author

Hey @luizcieslak, I didn't know you could do that with Chrome! Sorry but I don't know about Safari.

@david-baul
Copy link

I'm not the type to leave comments. Thank you so much. God bless you

@MarcoYolanga
Copy link

Thank you king

@johnny-mnemonica
Copy link

working on iOS 17.0.3, had to tweak the timeout values a bit but otherwise works beautifully, ty.
behaviour in safari is if user does not have the app installed, onFallback is triggered.
in chrome, onIgnored is triggered. onReturn fires as expected in both browsers

@cody-dashka
Copy link

cody-dashka commented Oct 27, 2023

try this.

function openDeepLink(url: string, options?: Partial<{ onOpen?: () => void; onFail?: () => void; waitTime?: number }>) {
  let timeout: NodeJS.Timeout;
  let interval: NodeJS.Timer;
  let visible: DocumentVisibilityState = 'visible';

   const handleOpen = () => {
      window.removeEventListener('visibilitychange', () => true);
      options?.onOpen?.();
  };
  const handleResponse = () => {
    if (visible === 'visible') return options?.onFail?.();
    clearInterval(interval);
    handleOpen();
  };

  try {
    window.addEventListener('visibilitychange', (e) => (visible = (e.target as Document)?.visibilityState));
    timeout = setTimeout(handleResponse, options?.waitTime || 5000);

    interval = setInterval(() => {
      if (visible === 'hidden') {
        clearTimeout(timeout);
        handleResponse();
      }
    }, options?.waitTime || 5000);

    window.location.href = url;
  } catch (error) {
    options?.onFail?.();
  }
}

openDeepLink("fb://profile/100004452732850", {onOpen: console.log, onFail: console.error});

Copy link

ghost commented Mar 25, 2024

Tested on last version of Firefox 124, Android 13. Not working because even if a dialog appears to redirect to the app, the pages doesn't loose focus.

@diachedelic
Copy link
Author

Can it be fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment