android -> native browser (non-chrome) -> no app -> web
-> yes app -> app
-> chrome browser -> no app -> web
-> yes app -> app
ios -> no app -> web
-> yes app -> app
other devices -> web
Redirecting the device browser to the custom app URL to open app
If the app is install on the device, the custom app URL will trigger the browser to open the app. It is used as a test for app availability.
set the browser location to a custom app url:
ios
someApp://
android
someApp://#Intent;scheme=someApp;end
/* or */
someApp://
ios: if the app is not installed on the device, safari will display an invalid path error.
android - chrome: if the app is not installed, chrome will display a website not found error for Chrome v.19+
Setting a cookie for flag persistance
Utilize a cookie to signify the app is not installed on the device to minimize the invalid path error on iOS Safari only once.
pseudocode
check noapp cookie
true
set browser location to web
false
set timer: web fallback
set noapp cookie with expiration
set browser location to web
attempt custom app url
success
redirect to app
fail (no app redirection)
timer complete -> web fallback
If the app is installed on the iPhone, user is redirected out of Safari into the app. If the fallback timer is short, the script continues until the device is completely switched into the app. Therefore, web fallback is triggered and noapp
cookie is set in safari during the app switch. Upon next visit, the user will go straight to web
since the noapp
cookie is set, even though the app is installed.
If the fallback timer is long enough, the script stops executing as soon as the device completely switches into the app, but if the app is not installed on the device, the invalid path error will be more noticeable for a longer time.
pagehide
event is triggered when a user gets redirected out of Safari and into the app through custom app URL.
To ensure noapp
cookie does not get set after app redirect on iOS Safari
Set appRedirect
flag upon pagehide
event trigger. pagehide
event will trigger upon leaving Safari. Then as the web fallback executes, appRedirect
flag is used to skip setting the noapp
cookie.
pagehide
event does not get triggered immediately upon leaving safari. About one second delay into the targeted app before pagehide
event is triggered in safari in the background.
If the fallback timer is short, the web fallback executes before the pagehide
event; the appRedirect
flag in web fallback does not reflect the actual behavior of the app switch.
if the fallback timer is set for more than 1000+ms, pagehide
event can be triggered, but noapp iOS users may see the error popup for a longer period, which makes for an insufficient user experience.
Also the pagehide
event delay unreliably varies depending upon the CPU load.
document.hidden
document.hidden
returns true
if the page is in a state considered to be hidden to the user, and false
otherwise. Suggested by an answer on stackoverflow.
website not found error still occured on Chrome
fallback chrome | chrome visibility doc | android chrome doc
place the custom app URL in the iframe src
.
It doesn't work on Chrome and invalid path error still occurs on iOS.
wrap the custrom app URL redirection in a try/catch.
the error happens on the platfrom native code, and javascript can't catch it.
same symptoms to attempt opening custom app URL
This should be obvious, but ajax is only for HTTP
request, but I thought I should try. So yup, someApp://
always fails.
- https://gist.github.com/pulletsforever/2662899
- http://stackoverflow.com/questions/525063/android-respond-to-url-in-intent
- http://stackoverflow.com/questions/2448213/how-to-implement-my-very-own-uri-schema-on-android/2448531#2448531
- http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser
- http://fokkezb.nl/2013/09/20/url-schemes-for-ios-and-android-2/
- http://stackoverflow.com/questions/4401764/what-event-fires-when-a-webkit-webapp-is-terminated
- http://stackoverflow.com/questions/7792519/catch-browser-closing-event-in-safari-running-in-iphone-ipad-etc
- http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app