Skip to content

Instantly share code, notes, and snippets.

@rsattar
Created November 1, 2011 21:20
Show Gist options
  • Save rsattar/1331949 to your computer and use it in GitHub Desktop.
Save rsattar/1331949 to your computer and use it in GitHub Desktop.
How to launch an iOS url-protocol-aware app via JS, but do something different if the app isn't installed
com.foo.MyApp.prototype.launchIOSAppOrITunesLink = function() {
// Listen for the Safari window to be deactivated
goog.events.listen(window, goog.events.EventType.BLUR, this.onMobileAppDetected, false, this);
// Launch the mobile app protocol and *ALSO* launch a timer
// that checks if the app was launched (it would have deactivated the mobile browser)
goog.Timer.callOnce(this.launchMobileApp, 100, this);
goog.Timer.callOnce(this.checkAppState, 600, this);
};
com.foo.MyApp.prototype.onMobileAppDetected = function() {
this.appDetected_ = true;
};
com.foo.MyApp.prototype.launchMobileApp = function() {
// Change your URL to a an url-protocol that you know is
// supported by your app:
window.location.href = "myiosapp://";
// Some other urls you can make:
// myiosapp://
// myiosapp://some/path/here
// myiosapp://?foo=1&bar=2
// myiosapp://some/path/here?foo=1&bar=2
};
com.foo.MyApp.prototype.checkAppState = function() {
if (!this.appDetected_) {
// We didn't deactivate immediately, so that app must not be installed
// Launch iTunes link or show some messaging to the user
} else {
// App is launching, so not much to do here
}
// cleanup our deactivation listener
goog.events.unlisten(window, goog.events.EventType.BLUR, this.onMobileAppDetected, false, this);
};
@rsattar
Copy link
Author

rsattar commented Nov 1, 2011

This is some code that was taken out of a google closure project, hence the goog.* calls

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