# Deep linking in Ionic and how to handle custom URLs

**URL:** <https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547>\
**Category:** tutorials\
**Created:** [March 3, 2015, 11:08pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547 "2015-03-03T23:08:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wedgybo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/wedgybo/32/64033_2.png) [@wedgybo](https://forum.ionicframework.com/u/wedgybo)\
**Post date:** [March 3, 2015, 11:08pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/1 "2015-03-03T23:08:07Z")

</div>

Hey all,

I’d noticed there’s been a few posts about deep linking and handling custom URLs with the handleOpenURL function recently. We’ve been cleaning up some code in this area so I thought I’d share some insight into how we handle it.

I’ve written up a small post on medium. Hope it helps someone.

> **[Deep linking in Ionic Mobile Applications – AngularJS Articles – Medium](https://medium.com/angularjs-articles/deep-linking-in-ionic-mobile-applications-44d8b4685bb3)**
>
> Deep linking inside of cordova apps build with angular has it’s challenges. This article details some clean solutions to these challenges

Cheers,  
Jamie

---

<div class="post-metadata">

**Author:** ![Mehul](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mehul/32/65127_2.png) [@Mehul](https://forum.ionicframework.com/u/Mehul)\
**Post date:** [March 3, 2015, 11:40pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/2 "2015-03-03T23:40:31Z")

</div>

I was just about to tackle doing this in my app, thanks!

---

<div class="post-metadata">

**Author:** ![RickW](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@RickW](https://forum.ionicframework.com/u/RickW)\
**Post date:** [March 5, 2015, 3:33pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/3 "2015-03-05T15:33:21Z")

</div>

Hi Jamie,

This is a really nice article - it’s a cleaner way of doing it than the other versions I’ve seen here, and it’s very clearly explained. However, I’m having one issue with it:

- if the app’s not running when I link into it, everything is fine
- if the app’s already running, the first deep link into it works fine. On subsequent links, though, I can see that the global openURL function gets called, but the OpenUrlService doesn’t pick it up the event so I don’t end up at the correct state.

I’m using ionic beta 14 and Cordova-ios 3.8.0. Am I doing something stupid somewhere in this? Is there something I don’t understand about Cordova’s event model?

Any advice or help would be much appreciated!

Rick

---

<div class="post-metadata">

**Author:** ![wedgybo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/wedgybo/32/64033_2.png) [@wedgybo](https://forum.ionicframework.com/u/wedgybo)\
**Post date:** [March 5, 2015, 4:07pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/4 "2015-03-05T16:07:25Z")

</div>

Ahhh we might need to add something to reset the trigger if it’s fired. Let me have a look into it.

---

<div class="post-metadata">

**Author:** ![chris08002](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chris08002/32/91975_2.png) [@chris08002](https://forum.ionicframework.com/u/chris08002)\
**Post date:** [April 20, 2015, 2:24pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/5 "2015-04-20T14:24:36Z")

</div>

Have you found any solution for this?

Chris

---

<div class="post-metadata">

**Author:** ![wedgybo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/wedgybo/32/64033_2.png) [@wedgybo](https://forum.ionicframework.com/u/wedgybo)\
**Post date:** [May 7, 2015, 9:53am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/6 "2015-05-07T09:53:48Z")

</div>

Hey @chris08002 So I finally got some time to go back to this and I’ve fixed it I think. You need to remove the sticky handler and reapply it again once you’ve opened the URL.

```
(function (window, document, angular) { 'use strict';

  angular.module('mlz.openurl', ['ionic'])
  .factory('OpenUrlService', ['$log', '$location', '$rootScope', '$ionicHistory', function ($log, $location, $rootScope, $ionicHistory) {

    var openUrl = function (url) {

      //window.alert('Opening 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) {
      // Check for the CustomEvent detail field for testing.
      // Difficult to trigger cordova events under test
      var url = e.url || e.detail.url;
      openUrl(url);
    };

    var onResume = function () {
      //window.alert('On resume handle url');
      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);
    }

  }]);

})(window, document, angular);
```

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [May 15, 2015, 12:27am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/7 "2015-05-15T00:27:10Z")

</div>

Is there a way to install the app by taking them to the appropriate location in Google Play or Apple App Store. Then proceed with the deep link?

---

<div class="post-metadata">

**Author:** ![wedgybo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/wedgybo/32/64033_2.png) [@wedgybo](https://forum.ionicframework.com/u/wedgybo)\
**Post date:** [May 15, 2015, 7:10am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/8 "2015-05-15T07:10:06Z")

</div>

I can’t find any information that would suggest this is possible. Unless you could figure out a way to take them to the store, install the app, then launch the app from your site again.

The problem is the app doesn’t launch from install and the app store doesn’t save the deep link when you direct people to the install page to pass on when you hit the open button.

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [May 15, 2015, 2:08pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/9 "2015-05-15T14:08:38Z")

</div>

I found an interesting article related to deferred deep linking. Im not sure if this company has a solution for this or not. I suspect they offer a “click” server. [http://blog.urx.com/urx-blog/2014/10/7/implementing-deferred-deep-linking](http://blog.urx.com/urx-blog/2014/10/7/implementing-deferred-deep-linking)

---

<div class="post-metadata">

**Author:** ![chris08002](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chris08002/32/91975_2.png) [@chris08002](https://forum.ionicframework.com/u/chris08002)\
**Post date:** [May 20, 2015, 7:48am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/10 "2015-05-20T07:48:25Z")

</div>

Thanks for this solution! It seems to work stable for us. I am now also looking into campaign referrals from the app stores.

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [May 28, 2015, 2:53am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/11 "2015-05-28T02:53:28Z")

</div>

[https://branch.io/](https://branch.io/) has a free service that provides among other things (deferred deep linking and deep linking). They also have a Cordova plugin. I am in the early stages of looking into this. I will post more information in case someone else is interested. [https://github.com/BranchMetrics/Branch-PhoneGap-Cordova-SDK](https://github.com/BranchMetrics/Branch-PhoneGap-Cordova-SDK)

---

<div class="post-metadata">

**Author:** ![chris08002](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chris08002/32/91975_2.png) [@chris08002](https://forum.ionicframework.com/u/chris08002)\
**Post date:** [May 28, 2015, 7:03am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/12 "2015-05-28T07:03:54Z")

</div>

I’ve been experimenting the last couple of days with [Branch.Io](http://Branch.Io). Looks like I got it to work now. I had some problems with the right sequence of initSession, closeSession, setIdentity… e.g. on a forced app close (android back button or task manager) the closeSession was not called and eventually params didn’t get caught at next start. Let me know if you want to share info on this one.

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [May 28, 2015, 12:57pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/13 "2015-05-28T12:57:22Z")

</div>

That would be great. Can you post some code on a gist perhaps?

---

<div class="post-metadata">

**Author:** ![chris08002](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chris08002/32/91975_2.png) [@chris08002](https://forum.ionicframework.com/u/chris08002)\
**Post date:** [May 28, 2015, 3:20pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/14 "2015-05-28T15:20:28Z")

</div>

Never done that, but I have given it a try 😄

> <https://gist.github.com/chris08002/331900fa2fe3ee9f739d>

Let me know if it is understandable

---

<div class="post-metadata">

**Author:** ![eno](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eno/32/64977_2.png) [@eno](https://forum.ionicframework.com/u/eno)\
**Post date:** [June 9, 2015, 7:26pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/15 "2015-06-09T19:26:53Z")

</div>

Im getting this error:

```
handleOpenURL is not defined

```

when I click on a custom URL.

---

<div class="post-metadata">

**Author:** ![eno](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eno/32/64977_2.png) [@eno](https://forum.ionicframework.com/u/eno)\
**Post date:** [June 9, 2015, 10:37pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/16 "2015-06-09T22:37:11Z")

</div>

Never mind, I realized I needed to define it properly (I had a typo).

---

<div class="post-metadata">

**Author:** ![eno](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eno/32/64977_2.png) [@eno](https://forum.ionicframework.com/u/eno)\
**Post date:** [June 9, 2015, 10:39pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/17 "2015-06-09T22:39:26Z")

</div>

Can you explain why we set `window.location.hash` ? Also wondering if we broadcast the event in the root scope, what handles that? What actually makes the state change? Im still trying to understand and get this working in my app (my app launches but doesn’t actually transition anywhere). Also noticed that the global `handleOpenURL` handler fires a `handleurl` event but the events in the service are all called `handleopenurl` ?

---

<div class="post-metadata">

**Author:** ![iqlas](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@iqlas](https://forum.ionicframework.com/u/iqlas)\
**Post date:** [June 9, 2015, 11:58pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/18 "2015-06-09T23:58:38Z")

</div>

We are looking at this…it seems [branch.io](http://branch.io) seems good (and is free for now)…can you post the code in detail. For now, when I click the link, nothing happens…

Thank you in advance…

---

<div class="post-metadata">

**Author:** ![maephet](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@maephet](https://forum.ionicframework.com/u/maephet)\
**Post date:** [June 10, 2015, 3:31am UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/19 "2015-06-10T03:31:48Z")

</div>

Hey team - I built a lot of the Branch service. Happy to help where I can. The repo that @keithdmoore linked to is outdated. [Here are the docs](https://github.com/BranchMetrics/Web-SDK/blob/master/CORDOVA_GUIDE.md) for the new, fully native SDK we built.

@chris08002 - did you figure out the issue with the session closing? We should fix the docs or otherwise if it’s incorrect.

---

<div class="post-metadata">

**Author:** ![wedgybo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/wedgybo/32/64033_2.png) [@wedgybo](https://forum.ionicframework.com/u/wedgybo)\
**Post date:** [June 10, 2015, 1:30pm UTC](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547/20 "2015-06-10T13:30:08Z")

</div>

I think I had issues with using the angular $window service which was the reason for using straight window. It’s been a while so it was probably something I was doing wrong and I never switched them back. You’re free to use whatever view switching method. Hash was just a quick way. You could use $state.go or whatever.

We broadcast on the rootScope so the app can listen for the event and hook into it. It’s not responsible for actually switching views and does nothing in the context of this example.

Good catch on the typo. I’ve updated the gist.

[Next page](https://forum.ionicframework.com/t/deep-linking-in-ionic-and-how-to-handle-custom-urls/18547.md?page=2)
