Adam Katz
Adam Katz

Reputation: 6962

React native (expo) how to define urls for ios

I have the following function that is supposed to open a link

const openBrowser = url => {
 WebBrowser.openBrowserAsync(url)
}

If the url is 'https://stackoverflow.com/questions/ask' it works fine, but if the user types in www.google.com it crashes on ios as it says, only http or https urls are supported.

I tried to change it to this

const openBrowser = url => {
 WebBrowser.openBrowserAsync(`https://${url}`)
}

now the www.google.com works, but now https://stackoverflow.com/questions/ask no longer works.

What is best way to handle all urls seemlessly.

Upvotes: 1

Views: 183

Answers (1)

Nils Kähler
Nils Kähler

Reputation: 3001

You should check if the url begins with http / https, and if it does dont prepend http://

function addhttp(url) {
   if (!/^(f|ht)tps?:\/\//i.test(url)) {
      url = "http://" + url;
   }
   return url;
}

Upvotes: 1

Related Questions