Open web url link in browser in Swift
Solution 1
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)
}
Solution 2
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)
}
}
Related videos on Youtube
Siddharth
I am passionate game developer whose hobby is to create games as well analysis and research in gaming field. I have more than 4 years of game development experience. During this time, I worked with AndEngine, libGDX, cocos2D and Unity game engines.
Updated on October 09, 2022Comments
-
Siddharth over 1 year
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:
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:
-
Siddharth almost 6 yearsalso can I make else if pair for executing any single line ??