iosangularionic4deeplink

My app is being opened by a URL scheme in iOS, how do I get the complete url?


In my iOS app, I'm opening an InAppBrowser with another app and ask a question. Then this second app is calling my app again with the scheme I configured (like myapp://), the complete url is something like myapp://something/:answer. I managed to open the app in the InAppBrowser and then my app reopen when it calls myapp://, but I need the full url so I can get the answer.

So far I tried everything I found with some examples, like https://ionicframework.com/docs/native/deeplinks and https://github.com/EddyVerbruggen/Custom-URL-scheme but with no luck.

With deeplinks, I've tried to follow the doc but the subscribe is never called and I can't see the console logs.

openSecondApp() {
  this.platform.ready().then( () => {
    if (this.platform.is('ios')) {
      const url = 'secondapp://link/question';

      const options: InAppBrowserOptions = {
        location : 'no',
        hidden : 'no',
        clearcache : 'yes',
        clearsessioncache : 'yes',
        closebuttoncaption : 'Close',
        disallowoverscroll : 'no',
        presentationstyle : 'pagesheet',
      };

      const browser = this.inAppBrowser.create(url, '_system');

      this.deeplinks.route({
        '/': 'ThisPage'
      }).subscribe(match => {
        console.log(match);
      }, nomatch => {
        console.log(nomatch);
      });
    }
  });
}

With custom url scheme I didn't understand where to put the handleOpenURL function. I tried to put it at the end of the <head> tag in index.html:

function handleOpenURL(url) {
  console.log("url: " + url);
}

but it never gets called.

(I'm on iOS, I'm using Ionic4 with capacitor.)

Can someone share an example on how to do this?


Solution

  • You have to use capacitor API

    import { Plugins } from '@capacitor/core';
    
    const { App } = Plugins;
    
    App.addListener('appUrlOpen', (data: any) => {
      // data.url contains the url that is opening your app
    });