Skip to content

Instantly share code, notes, and snippets.

@wedgybo
Last active January 7, 2016 20:23
Show Gist options
  • Save wedgybo/dce87f3ac06c4f18bd3b to your computer and use it in GitHub Desktop.
Save wedgybo/dce87f3ac06c4f18bd3b to your computer and use it in GitHub Desktop.
AngularJS service to listen for app opens via a custom URL
angular.module('mlz.openurl', [])
.factory('OpenUrlService', ['$log', '$location', '$rootScope', '$ionicHistory', function ($log, $location, $rootScope, $ionicHistory) {
var openUrl = function (url) {
$log.debug('Handling open URL ' + url);
// Stop it from caching the first view as one to return when the app opens
$ionicHistory.nextViewOptions({
historyRoot: true,
disableBack: true,
disableAnimation: true
});
if (url) {
window.location.hash = url.substr(5);
$rootScope.$broadcast('handleopenurl', url);
window.cordova.removeDocumentEventHandler('handleopenurl');
window.cordova.addStickyDocumentEventHandler('handleopenurl');
document.removeEventListener('handleopenurl', handleOpenUrl);
}
};
var handleOpenUrl = function (e) {
openUrl(e.url);
};
var onResume = function () {
document.addEventListener('handleopenurl', handleOpenUrl, false);
};
return {
handleOpenUrl: handleOpenUrl,
onResume: onResume
};
}]).run(['OpenUrlService', function (OpenUrlService) {
if (OpenUrlService) {
document.addEventListener('handleopenurl', OpenUrlService.handleOpenUrl, false);
document.addEventListener('resume', OpenUrlService.onResume, false);
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment