Learn how to launch the Settings app directly from an app.
- [Instructor] In a previous tip, you saw how to add a settings bundle. We had to go to the settings bundle by launching the application manually. This week, I'll show the really short way to launch the Settings app in a simple application. Download the starter file, which is a modified version of the Settings Bundle exercise. I added an extra button on the bottom here, which I'll use to launch the app settings. And if you're getting this blue business here, you can just go to Assets, hit the Pizza, and go back, and you'll see that it's really there.
I'll head over to the View Controller here, and you'll see I've set up launchSettings as an action already. But underneath that and between it and viewDidLoad, I'm going to make a new function called settingsLaunch. Now there's two things we're going to need here, and you can launch any app using the open method of UIApplication, but that's going to need a URL. So what I'm going to do first is I'm going to do if let appSettingsURL equals URL.
And I'll use the string version of this, which is an optional, and which is why I'm using the if let here. And in the string here, there's a UIApplication string constant that we're going to be using called openSettingsURLString. That gets the URL for the app for settings. Inside of here, what I'll do is then use appSettingsURL.
And for that, UIApplication again. I'll go to its singleton shared, open, you'll see we have some parameters here. The first one is the URL, which I'll use appSettingsURL. The options here, I'm going to hit return first and just get rid of this. We're going to have no options whatsoever. So blank options is just that.
And then for the completion handler, you don't really need it but I think it's always a good idea to put it in here. If you can't launch the settings app, which is what this all is going to be about, you have a lot more to worry about than your current running app. Go ahead and hit Return over this and you'll see that you get a Bool for a parameter. Make that success. And then in the code, just print an error message. So if not success, print Unable to launch.
You can get that more interesting if you want. But I'm not going to get too upset about it. And now with this one little method, I can add that to my launchSettings up here. And I'm ready to run. So I'm going to use an iPhone XR here. And just to make sure everyone's on the same page, I'm going to Open Developer Tool, Simulator, I got an iPhone XR up here.
And once it loads, I'm going to go to hardware, Erase All Content and Settings, and Erase. And then wait a minute here 'til it resets. And now I'm sure that everything in my settings is clear so this will work perfectly. And everyone will get the same results. I can go back to my code now, run the app.
I can hit the Settings button. And I pop myself directly into the settings so I can go ahead and change some settings here. I can make this cheese and peppers. And check the size, small. And I can go back using the little part here. And there it is. With only a little bit of work, you can get to the application settings app from your app.