On leaving the app to view a URL

Do something when a user views a URL from a native mobile app

By default, if a user attempts to go to a URL on a mobile device, the URL will open within the app (ie, it will not be opened by the mobile’s browser such as Safari or Chrome). If you don’t want this to happen you can specify that a URL is opened in the user’s default browser with the following code:

JavaScript

Fliplet.Navigate.url({
  url: 'https://fliplet.com',
  inAppBrowser: false
});

To do something when a user is about to open a URL you can register a ‘hook’ to run some custom code before the URL is opened. This is useful if you want to make changes to the URL (for example change the parameters of the URL to help with analytics). The code below demonstrates changing the destination URL parameter.

JavaScript

Fliplet.Hooks.on('beforeNavigateToURL', function (data) {
  // You can also change any of the input data if you need to
  data.url = 'https://example.org?search=hello';
});

The code below demonstrates how you can prevent the navigation from happening by rejecting a promise.

JavaScript

Fliplet.Hooks.on('beforeNavigateToURL', function (data) {
  // Prevent users from navigating to a certain URL
  if (data.url === 'https://example.org') {
    return Promise.reject('Forbidden URL');
  }
});

Another example would be we simply force all ‘linkedin.com’ and ‘twitter.com’ URLs to open with the system browser (so that the installed Twitter or LinkedIn apps get opened) instead of in the in-app browser:

JavaScript

Fliplet.Hooks.on('beforeNavigateToURL', function (data) {
  var link = document.createElement('A');

  link.href = data.url;

  if (link.host.match(/(linkedin\.com|twitter\.com)$/i)) {
    data.inAppBrowser = false;
  }
});