Redirecting from Facebook/Instagram in-app browser to default browser on iOS

I am trying to create a redirect to the default browser if a user opens my website link using the Facebook or Instagram in-app browser.

So far, my solution works on Android, where a dialog box informs the user that they are about to leave the app, and the user has to click “Continue.” This behavior is acceptable for my needs.

However, on iOS/iPhone, my code doesn’t seem to work at all. I can’t figure out how to make the code work on iOS as well. I suspect it might be related to the fact that iOS does not support the googlechrome:// scheme in the same straightforward way Android supports intent://. I tried using http:// as a fallback, but that didn’t work either.

Here is my current code:

(function() {
    function isFacebookOrInstagramApp() {
        var ua = navigator.userAgent || navigator.vendor || window.opera;
        return (ua.indexOf("FBAN") > -1) || (ua.indexOf("FBAV") > -1) || (ua.indexOf("Instagram") > -1);
    }

    if (isFacebookOrInstagramApp()) {
        var url = window.location.href;
        var iosUrl = 'googlechrome://' + url.replace(/^https?:///, '');
        var androidUrl = 'intent://' + url.replace(/^https?:///, '') + '#Intent;scheme=http;package=com.android.chrome;end';

        // Check if the device is iOS or Android
        if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
            window.location.href = iosUrl;
        } else if (/Android/i.test(navigator.userAgent)) {
            window.location.href = androidUrl;
        } else {
            // Fallback for other devices
            window.location.href = 'http://' + url.replace(/^https?:///, '');
        }
    }
})();