Objective-C iPhone Programming Lesson 21 - Opening a URL In Safari
Hey guys, this is Matt. Heads on one with another iPhone programming tutorial. In this video, I'm going to be both reviewing a last tutorial's challenge, which I'll go over in a second, and I'm going to be showing you how to open a URL when the user presses on a button.
So, the challenge from the previous tutorial was to make a segmented control with code without using any interface builder. I’m just going to be showing you how to do this quickly. We're not going to be recreating our project completely from the last tutorial, but I am going to be creating this segmented control with code just so you can see how it works.
So, next, we’re going to create a new project, or you can use the existing one if you still have it, and I’ll call it "segment with code." Alright, it's going to come up. I’m gonna declare a UI segmented control control, and I’m gonna make a void control changed. Alright, now I'll go into the implementation and put this right here.
In our viewDidLoad, like normal, we’re going to be creating our control. So, we’re going to say control equals UI segmented control alloc and init with items. Now, items takes an array of strings, so we’re just gonna create an array. So, we’ll call it my items, and now up here, we're gonna declare my items.
So, we say my items equals NSArray array with objects. Alright, and these are gonna be "first," "second," "third," and then after all those you put nil to terminate the sequence. So, "first," comma, "second," comma, "third," comma, nil. Alright, so we’re giving it my items. Now, we have to give it a frame. So, we’ll say control setFrame CGRectMake(10, 10, self.view.frame.size.width - 20, control.frame.size.height).
The reason I'm doing this is because segmented controls look better at a certain height than pretty much any other height. When you create them like this, they'll have a default frame that has this height. So, we’ll just keep this height from its old frame, that way it'll have the right height and it looks nice.
Now, we’re going to say control setSelectedSegmentIndex 0. This just selects something by default. Then, control addTarget self action: selector, we're gonna give it control changed. This is our method name. As we look up here, this is the method name. And then for control events UIControlEventValueChanged. This means that this method will get called whenever the value is changed of the controller.
This is pretty much always what you'll use for a segmented control, and you’ll pretty much never use it for a button or anything like that. So, now we have to take the final step and add the controller to our view. So, self.view addSubview control.
Now, let’s review what this does real quick. Let me just resize this so you can see all the code it wants. So, first, we're creating an array with all of the titles that we want to have on our buttons. These are NSStrings, which can be denoted with the @ sign and then the quote, so they’re separated by commas, and then at the end, there’s a nil to terminate the sequence.
This creates a new segmented control with those items. Now, when it’s created, it has no idea where on the screen it needs to be. It just knows how tall it wants to be, but it doesn’t know how wide it needs to be or where it needs to be. So, this gives it its x coordinates from the top left and its y-coordinates from the top left.
This gives it the width of the entire view minus 20. Now, we’re making it minus 20 because we want it to be the width of the view but where there’s a 10-pixel padding on either side, so we subtract 20 and then we give it its original height. Then, we select the default item. This will be the first one because it's zero. We set up our event, and then we add it in our event.
I’ll have an NSLog item changed, and obviously, you can plug in the code here that we had in our previous tutorial. So, if we run this, we will see that our segmented control is on the screen, and it changes. We get a little message in our console whenever it changes, as you can see. So, that is the segmented controller with code.
Now, time for the topic of this tutorial that I want to cover. A lot of people have asked me in the past how to make it so when the user clicks on a button or a link in the app, it opens that URL in Safari. The answer to this is actually very simple because Apple already has an API to do this in one or two lines of code.
So, let’s go into Xcode and create a new file, new project, and I will call this "open URL." Alright, so this is just going to be a really simple view with one button. Alright, when they hit the button, it'll open a URL in Mobile Safari.
So, let's open up the view controller in Interface Builder. Okay, alright, so here it is. Now, let’s drag on a button, and I’ll just make this button say "go to Google." Alright, and this button is going to make us open Google in Safari.
So, now let’s go to open URL view controller and declare IBAction goToURL (id)sender. And we’ll go into the implementation and throw this in there. Now, let's go back into Interface Builder and hook up this button.
So, we'll hold control, click down on this, drag it to files owner, say goToURL, and now we’ll save in Interface Builder and we will quit. Alright, so now here’s the actual code that will get called when they hit the button, and we’re gonna make this just open a URL in Safari.
So first of all, we’ll make a URL object, I suppose. MyURL equals NSURL URLWithString, and then here in the quote we'll put the URL. In this case, it's Google, so "http://www.google.com/mapmaker."
And say UIApplication sharedApplication openURL: myURL. Now, this is pretty simple. You probably understand what this code is doing, but NSURL is an actual class. It's a type of object that represents a web URL. A string such as this is not a URL; it's just a piece of text that means nothing.
Well, it needs something, but it doesn't mean that it's a web address. So, in order to get it into a URL object, we use the URLWithString method on the NSURL class. So that gives us a URL; we're storing it into the variable myURL.
Now, every app has a shared application object, which lets you do various tasks like set a badge number, set like progress indicators at the orientation of your app, and it also happens to be able to open a URL. It’ll open the URL normally in Safari. You can have a URL that links to an email address, and it'll open in Mail, stuff like that.
So, let’s go ahead and run this in the iPhone simulator. Alright, so here’s our app. We click on "go to URL," and it opens right up in Safari. So here you can see here's Google's homepage. We can go back to our app, whatever, and here’s our app.
So, that’s how to do it. It's pretty simple; pretty much anyone can do it. It doesn’t take much. It's literally two lines of code. You can make it into one line of code. Let me show you how to do that.
So this is just a shortcut. If you don’t want to have to declare this variable, you can just copy this and throw it here and then get rid of this. And now it does the same thing. If I just build it to prove it to you that it works, it does the same thing, except that it's one line of code.
So, that is how to open a URL from an iPhone app in Safari. So, thanks for watching Matt Heads 101. Subscribe and goodbye.