In this post we will learn how to open website in iOS browser.
We will open Facebook in iOS browser.
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “OpenBrowser”
Step 2 − Open Main.storyboard and add a button as shown below. I have given the button title as “Open Facebook”
Step 3 − Attach one @IBAction function in ViewController, name it openBrowser
Step 4 − In openBrowserFunction write the code to open URL like shown below
@IBAction func openBrowsere(_ sender: Any) { let url = URL(string: "https://www.facebook.com")! if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } }
In the above code as you can see we are using canOpenURL and openURL APIs.
canOpenURL checks whether the given URL can be opened by the app. Then openURL actually opens the given URL with appropriate app.
Step 5 − Run the app. Click on open Facebook button. You should see the browser opening the Facebook page.