Search code examples
ionic-frameworkcordova-pluginsdeep-linking

Launching Ionic app routing


Found this solution to lounching app via plugin

.run(['$state', '$window',
function($state, $window) {
    $window.addEventListener('LaunchUrl', function(event) {
        // gets page name from url
        var page =/.*:[/]{2}([^?]*)[?]?(.*)/.exec(event.detail.url)[1];
        // redirects to page specified in url
        $state.go('tab.'+ page, {});
    });
}]);

function handleOpenURL(url) {
setTimeout( function() {
    var event = new CustomEvent('LaunchUrl', {detail: {'url': url}});
    window.dispatchEvent(event);
}, 0);}

This solution works great if use simply state name, ex. myapp://posts

.state('app.posts', {
url: "/posts",
views: {
  'menuContent': {
    templateUrl: "templates/posts.html",
    controller: 'PostsCtrl'
  }
}})

open app/posts.

But I need open state like this

.state('app.post', {
url: "/posts/:postId",
views: {
  'menuContent': {
    templateUrl: "templates/post.html",
    controller: 'PostCtrl'
  }
}})

with link myapp://post/2525. Can somebody help with this? Thank you!


Solution

  • Solved myself )))

    .run(['$state', '$window',
    function($state, $window) {
        $window.addEventListener('LaunchUrl', function(event) {
            // gets page name from url
            var page =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[1];
            var id =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[2];
            // redirects to page specified in url
            // alert ("id:" +id);
            $state.go('app.'+ page, {'postId': + id});
        });
    }]);