Hello and welcome to our very first episode of Route 85,
our new show just for you iOS developers.
Ooh, do you smell that?
It's still got the new show smell.
That's nice.
Anyway, as you probably know, the Chrome web browser
has been available on iOS devices for a few years now
but being able to select your default browser has not.
So if you're a regular Chrome user on iOS, like I am well,
you've probably got a whole bunch of tabs open in Chrome
because that's the browser you use every day.
But you've also got some pages open in Safari because that's
the browser that opens by default
when you typically click on a link in another app.
But I wanted to show you something here.
Take a look at this.
I'm here using my favorite Twitter client, Tweetbot,
which I highly recommend if you haven't seen it yet.
And here I see that Colt McAnlis has added a link
to his perflikeapirate day talk.
Now when I click on it, it opens in Safari, as you might expect.
But now let's go back to Tweetbot here
and go into my preferences over here.
And you'll notice that one of the options I have here
is to change my default browser.
And one of those options is Chrome so,
well heck, let's pick that.
We'll go back and now watch what happens
when I click on this very same link.
Well now it opens in Chrome along with the 24 other tabs
I have open.
I have a lot of tabs.
And incidentally, this provides me with a few other benefits
as well.
If this site required a log in and password and Chrome
had remembered it for me from, say, the desktop version,
I'd be able to sign in using that password without having
to type it in.
If you look at the settings you'll
see that over here I also have Chrome's Data Usage
option turned on, which does allow
me to reduce my bandwidth if I am browsing
on a mobile network instead of Wi-Fi.
It also turns out that if this page were in another language
Chrome would translate it for me.
And of course, if I were to bookmark this article
you would see that it would show up later on my desktop browser
if I wanted.
But I think the nicest benefit of all,
particularly for you app developers,
is that Chrome gives me this nifty little Back button here
that, when I click on it, goes right back to my Tweetbot app
where I left off.
So I can continue checking on my Twitter news.
So how do they do that?
Well, while I haven't seen their source code
I've got a pretty good idea of how they pulled this off.
So this involves a technique in iOS known as URL schemes.
The idea is that if you were to open a URL,
like the ones pictured here, iOS would
look at that little bit of text before the colon and use that
to decide what app it should open
to process the rest of the URL.
In this first example, for instance,
mailto would open in iOS's mail app.
Similarly, tel would open phone, http and https
would open in Safari, and in one kind of special case,
iOS will take any web link that starts with maps.apple.com
and open it directly in Apple's Maps app.
Now as third party developers we can't
change what apps get to open mailto, http, or tel URL
schemes but we can declare our own schemes.
By doing so we then declare that a URL that
starts with that string belongs to our application.
If we do, and then the user then attempts
to open that kind of URL, our application gets opened
and this URL is passed to our own app delegate to handle.
So we could create an app that says
that a URL beginning with foo opens up in our application.
And in fact, we'll show you how to do this at a later point.
But I think the important thing to know here
is that Google Chrome, along with Maps,
and many other third party applications
have declared their own URL schemes.
And you can take advantage of this
to open a URL directly in Chrome.
Let's find out how.
So let's start by creating a simple application.
Now at the time of this recording Swift and Xcode 6
are still in beta so I'm going to do
this in Objective C and Xcode 5.
Now if there's enough interest I can certainly
redo these tutorials in Swift as well.
That's a little something I like to call job security.
So I'm going to create a new project here
and I'm going to make it a Single View Application.
Let's call it OpeninChromeExample
because I'm really original like that.
We're just going to make it for the iPhone for simplicity.
And feel free to create it wherever you'd like.
Now let's go into our storyboard here
and I'm going to add a button that will say oh,
let's just have it say Open a web page.
Center it.
Gosh, look at this application.
Isn't it beautiful?
This thing is beautiful.
Oh man.
Anyway, I'm going to open up my helper editor here
and I'm going to Control-drag from this button into here.
And I'll create a new Action called openWebPageWasPressed.
So now from within here I'll create an NSURL.
We'll call it googleURL and it will
be an NSURL, URLWithString, and in here we'll
do https://www.google.com.
Feel free to replace this with your own favorite web page
if you get tired of seeing the Google Home
screen over and over.
And I'm going to have my UIApplication sharedApplication
call openURL and I'll have it open googleURL.
There we go.
And sure enough-- let's save this--
when I run this application, and I click on this button,
there you go, it opens the Google Home page in Safari.
And that's because the URL I'm trying to open right now
begins with https, which, as you'll recall,
iOS automatically assigns to Safari browser.
So now let's go back into my application here.
I'm going to go into my View Controller and the main editor
and let's watch what happens when I replace https
with Google Chrome.
Now when I run this, and I click on this button, huh,
nothing happened.
And that's because I'm running this on the simulator
where I don't have Chrome installed.
Now this is a situation I should check for
and I probably want to fall back to Safari if the user doesn't
have Chrome installed, and we'll get to that later.
But for now let's stop this.
And I'm going to switch over from the simulator
to my actual iPhone and run it there.
And now when you click on this button
we see that, sure enough, my URL now opens in Google Chrome.
Hooray.
And honestly, at the most basic level
this is all you really need to do, replace http
with Google Chrome and replace https with, I guess,
let's make this Google Chrome s to open it securely.
And voila, all your links open with Chrome.
But let's go back to the app running on my simulator
where I didn't have Chrome installed.
Obviously, having my button click
do nothing is a bad user experience.
At the very least, I should check for this situation
and open the link in Safari instead.
And it turns out, there's an easy way to do this.
In addition to UIApplications openURL method,
it also has a Boolean canOpenURL method
that checks to see-- with the system--
to see if there's an application out there that
can respond to this URL scheme.
So I'm going to go back to my button handler here
and I'm going to create two URLs.
The first one, that I will call mySafariURL,
it is an NSURL URLWithString, and here we're
just going use the plain old https URL scheme.
And this one I'm going to rename as our Chrome URL.
And here we're using the Google Chrome s URL scheme.
Then down here I'll simply check and see
if my UIApplication sharedApplication canOpenURL
chromeURL then UIApplication sharedApplication openURL.
And otherwise, we will default, or I guess fall back,
to the Safari URL.
So this time the application does the right thing
on the simulator.
I click my fancy Open a web page button
and it opens the google.com Home page in Safari.
And when I go back to my actual device,
and on my device, when I click on this button, sure enough,
it now properly opens in Chrome.
So this is great.
But I'd like to explore one other feature
that we haven't really covered yet
and that's probably the one most important to app
developers, which is the ability to add that nice little back
button in Chrome that takes users back to your application.
I know it seems like a small thing
but being able to click back with one press
as opposed to double clicking the Home button
and then selecting your app again from the multitasker
just feels like a much smoother experience to me.
And we will show you how to do that in the very next lesson.
So stick around.
[MUSIC PLAYING]