Last active
September 30, 2020 15:51
-
-
Save aburnett/57f315f57bf29c6adb0c17e1580b2735 to your computer and use it in GitHub Desktop.
Open Facebook app on iOS if it exists, falling back to browser otherwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function preventPopup(timeout) { | |
clearTimeout(timeout); | |
timeout = null; | |
window.removeEventListener("pagehide", preventPopup); | |
} | |
function openFacebook(site, platform) { | |
let fbAppUrl; | |
switch (platform.type) { | |
case "ios": | |
fbAppUrl = `fb://page/?id=${site.thirdpartyId}`; | |
break; | |
case "android": | |
// On android the facebook app seems to pick | |
// up the fallback url instead of this one | |
fbAppUrl = `fb://page/${site.thirdpartyId}`; | |
break; | |
} | |
window.addEventListener("pagehide", preventPopup); | |
document.addEventListener("pagehide", preventPopup); | |
if (fbAppUrl) { | |
console.log("opening fb app " + fbAppUrl); | |
// try opening FB APP url in iframe so we don't show error dialogs | |
// if the app isn't installed | |
// var iframe = document.createElement("iframe"); | |
// document.body.appendChild(iframe); | |
// iframe.setAttribute("style", "display:none;"); | |
// iframe.src = fbAppUrl; | |
// 9/28/2020 - sneaking the app load in an iframe was no longer working | |
// just open the app directly in this window so the user sees the | |
//"are you sure" dialog on ios | |
window.location = fbAppUrl; | |
setTimeout(function () { | |
//this fallback will catch if the app doesn't open | |
window.location = site.url; | |
}, 1000); | |
} else { | |
window.location = site.url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment