Sitemap
TanyeliSargut

TanyeliSargut

System View Controllers

3 min readJul 18, 2022

SFSafariViewController

When you create an app, you have to predict some situations upfront. what I mean by that is, for example, opening up a link or web page on your app, if you redirect the user to Safari directly, that would be a mistake. Users tend to distract. You have to keep their attention on your app. But if you redirect them to Safari, they might see an old tab that can remind them work has to be done. You might think what’s the harm but as a result of it, you will lose your user.

Apple has a great tool for these situations, which is named SFSafariViewController. This tool helps us to open web pages without leaving the application. And, it’s pretty easy to implement it.

Get TanyeliSargut’s stories in your inbox

Join Medium for free to get updates from this writer.

In this case, I won’t design a page to keep it simple but you can easily adapt it to your design and app.

Secondly, we open the ViewController that our safariButton will be in. Create an @IBAction for our button. And, connect it to our page.

Press enter or click to view image in full size

And, most importantly, we import SafariServices.

Let’s start coding. There are two ways to do it. One of them is one line of code shorter but not a safe way to do it and another one is a safe way to do it. First, I will show you the safe way and I suggest you use this way. In case of misspelling the link etc., this way will save your app from crashing. Let’s see the safe way.

Press enter or click to view image in full size

When we run the app, as you can see below, our link opens and works perfectly.

Lastly, I will show you the not-safe method. The only difference is the unwrapping part. In this way, we will force unwrapping. As I said before I’m showing this to you because I want you to know both ways but I don’t suggest you use this way.

Press enter or click to view image in full size

It’s all I can say for this article today. I hope this article will help you with your project.

--

--