Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open web url link in browser in Swift

I want to load webpage that link I fetched through web service. I have tried myself but result is not in favour.

From server I was getting this kind of url: http://www.simrishamn.se/sv/kultur_fritid/osterlens_museum/

Also I have used this type of code to load the page:

let url = URL(string: "http://" + urlSting.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!)
UIApplication.shared.openURL(url!)

But when I test this code in device, its showing like this:

enter image description here

Please give some help to load fetched web url.

EDIT: After doing suggested changes, not web browser not opening though button click event is working, check below image for more clearance:

enter image description here

like image 212
Siddharth Avatar asked Jun 27 '18 17:06

Siddharth


People also ask

How do I open Chrome in Swift?

let chromeURL = "googlechrome://https://www.swift.com" UIApplication. shared. openURL(URL(string: chromeURL)!)


2 Answers

You need to check

like this

if let url = URL(string: urlSting.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url)
}

Also try removing and/or adding https:// prefix,
and if does not work then simply do:

if let url = URL(string: urlSting), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url)
}
like image 117
Abhishek Thapliyal Avatar answered Sep 17 '22 17:09

Abhishek Thapliyal


Please try this.

if let url = URL(string: urlString), UIApplication.shared.canOpenURL(url) {
   if #available(iOS 10.0, *) {
      UIApplication.shared.open(url, options: [:], completionHandler: nil)
   } else {                                            
      UIApplication.shared.openURL(url)
   }
}
like image 34
Pooja Singh Avatar answered Sep 18 '22 17:09

Pooja Singh