Join Todd Perkins for an in-depth discussion in this video Sandboxing an application, part of Cocoa with Swift Essential Training.
If you were to download a simple application from the App Store, for example, a calculator. Do you want the calculator to have access to connect to the internet, to have full access to read and write all through your file system, to access your camera and everything else on your computer, that you're allowed to access as a user. Now of course not, that's kind of weird, right? Well up until recently, that was the situation for Cocoa applications.
Now there's something that is actually required to release an app on the Mac app store or to sign it with your developer ID that's called sandboxing. Sandboxing says you have to explicitly say what features your app is going to use, otherwise, the app cannot use those. For example, my app is going to reference web data, my app is going to use the camera, or reference the documents folder, etc. So let's look at how that works. Now I have a blank window here and I am going to drop in a webView.
A webView can show a website. So I'm going to fill the whole screen with this, and actually I'll make the window a little bit bigger. And there's my webView. Now I want to connect this to my code, so I'll use the assistant editor. And I have AppDelegate on the right, my webView on the left and I'll ctrl+drag into my file. Have this be an outlet, I'll call it webView and then click connect. Now you may notice that there is an error here. So let's go back to AppDelegate.swift and turn off the assistant editor.
And if you have an error just make sure to import WebKit. So webView is actually in that framework, so once you do that your error should go away. And now an application did finish launching. I actually have a couple of lines of code that I've written. This line is line 19, creates a URL request to a website and we want to show this website inside of our webView. So I'm going to uncomment line 20. And this is the line of code that actually loads the URL into the webView.
So let's see what we get. Just as you might expect, the web page loads just fine. So I'll close that window and we'll look at how sandboxing can affect our application. So let's click on our project name in the navigator. And then make sure that you're on your target rather than your project. Your target should have the application icon next to it. So when you're on your target click the Capabilities tab and in there you'll see what your device is allowed to do. So here you can enable the app sandbox, which we're going to talk about in just a second.
You can enable iCloud, Game Center, etc. So let's expand app sandbox and take a look at it. It says here that it employs services of the operating system to create a barrier between your application code and critical user data. So let's turn that on. I'm going to click the button. And when I turn it on, you'll see the settings here change, and then sandboxing.entitlements is added to my project. We'll look at that file in just a minute. Once we've enabled sandboxing, we can then explicitly say what features our app is going to use, even down to which folders it's going to access and if it's going to read only or read write access those folders.
So, now with the sandboxing enabled and none of these options checked, I'm going to run the application and see what happens. So I run the app and the page actually does not load. And that's because I haven't given the application permission to do that. Notice that there's no error. The application does not crash. That's because Apple doesn't want the user to be bombarded with error messages and crashes in the event of Sandbox issues. So we'll close that and stop the application. Just to look at sandboxing.entitlements really fast, this just adds the app sandbox entitlement to the file with a boolean of yes.
Just saying the app is going to utilize the app sandbox. So back to my project and since our application is actually going to use outgoing connections, I'm going to check the box and I'll run the application again. And there it is. It loads just fine. So with the sandbox, I can look at what my application is going to access. And again, this is required if you're going to publish your app in the Mac App Store. And I can define exactly which features it's going to use and which ones it's not going to use.
- Creating your first Cocoa app
- Understanding the relationship between Cocoa and Swift
- Creating a playground
- Working with variables, functions, arrays, and loops
- Creating basic interactions and quick connections
- Building custom controller classes
- Using delegates
- Creating and arranging interface elements
- Using data controls
- Debugging and troubleshooting
- Distributing a Cocoa application
Skill Level Beginner
Q: This course was updated on 5/03/2016. What changed?
A: We added one new tutorial explaining how the Swift 2 and Xcode 7 updates affect the training in this course (which was recorded using Swift 1 and Xcode 6). The changes are minor, so Todd provides some short tips for using optionals, coding with NSDate() and NSURL(string:), and working with other smaller changes in the framework.