Join Bill Weinman for an in-depth discussion in this video Understanding URL schemes, part of iOS 6 App Development: System Resources.
In iOS, URLs are used for launching one application from another. The most simple example of this is launching a web page in Safari. Let's take a look at how this is done. Here we have open in Xcode, a working copy of URLScheme-start from the chapter four folder of your exercise files. Let's take a look at the storyboard. It has a place to type a URL. It has a Launch button. And it has a label that's used to title the page, with the words URL Scheme. Now coming back into our code, we see that we have an IBOutlet for the text field for typing in the URL.
And we have an IBAction for the Launch button. So the first thing we need to do is to provide a method for launching a URL. URL is launched off of a UIApplication instance. The UIApplication class provides a centralized point of control for all iOS applications. It's a singleton and every application has a singleton instance of its own UIApplication object. So we're going to go ahead and create a place for that in our interface here. This is a local interface. It's not a public interface. UIApplication, and call it application like that.
Now we're going to come down here to our Launch button. Really this is very simple. We're going to test and see if our local copy of the UIApplication object has been set up by typing if application like that. And if it hasn't been, then we're going to initialize it like this. SharedApplication is a class method for the UIApplication object, and it simply grabs a copy of that singleton for our application. Very simple. Now we use that application object.
OpenURL takes an NSURL object, so I'm going to create a temporary object with NSURL and URLWithString, it initializes the URL with a string, and pass it the URLField.text, which is the text string from our URL field. I'm going to press Shift+Cmd+K to clean, Cmd+B to build, and Cmd+R to run this in the simulator. I'm going to type a URL. The first part of the URL, the part that says HTTP before the colon //, that's called a scheme.
And the HTTP scheme in iOS is handled by the Safari web browser. So when I pass a URL with an HTTP scheme to openURL, it will launch the web browser. It will launch Safari. Bw.org is my own website. So when I press the Launch button here, it launches Safari and it brings up my website. So you can see it's very simple. It's very little code to launch a URL and an iOS app. URLs are used for more than just launching webpages.
In the next movie, I'll show you how to launch the iOS mail app to send an email. And later in this chapter I'll show you how to register a custom scheme for your own apps.
- Preparing your app for internationalization
- Localizing strings and images
- Taking photos or video with the camera
- Reading records from the Address Book
- Registering a custom URL scheme
- Getting a device's location
- Reading raw motion data
- Posting to a specific social media outlet
- Sending messages with the Message UI framework