of>> On this week Xamarin Essential API of the Week,
I'll show you how to open the Browser,
directly from your mobile app.
[MUSIC]
>> Welcome back everyone to
another Xamarin Essential API of the Week.
Today, we are going to take a look at the Open Browser API,
part of Xamarin Essential,
that helps you launch the browser application to a specific URL.
What's really cool is that you can use the default launcher or use
the system optimized browsers such as
Chrome Custom Tabs and SF Safari View.
So, let's take a look to see how to implement it.
Here's my sample application that I've been using for
all of the Xamarin Essential API videos.
What I've done is I've added a button or
a label that can be clicked for forgot password.
Now, here you'd probably want to launch and
open up a forgot password website,
so your users can reset their password.
So, to get started with Xamarin Essentials,
we're going to head over to our documentation,
just like we always do.
Ensure that you open up the "Getting Started" guide to
install Xamarin Essentials and
then you can browse through all of the feature guides.
Here is the "Open Browser" guide.
Now it's really easy to get started,
because all you have to do is call OpenAsync and passing
a few commands to it and that will automatically launch the URL,
which is either a string or a URI to the browser.
So, let's see how we can implement this.
Over in our code, I've gone ahead and
installed Xamarin Essentials into all of my projects,
and inside of my main activity,
I've of course added my Xamarin Essentials initialization code
and my code for permissions here on Android,
and that's in the "Getting Started" guide,
and of course I'll put links in the show notes for this.
On our login page,
we have this label.
This label says forgot password.
The text color is blue,
has the underline and then I have
a tap gesture recognizer for the label that when clicked,
will go ahead and get executed.
So, now over in the code behind for this page,
I have that forgot password tapped.
Now, for any Xamarin Essential API,
we're going to go and once I add
the using statement for Xamarin Essentials.
Else down here, will see that I could type in xamarin.essentials.
If I zoom in, we'll see that I get all of
the APIs available in Xamarin Essentials,
and here is the browser API.
So, instead of adding the Xamarin Essentials
since we added that using statement, I'll say,
dot, and inside my IntelliSense, I'll see "OpenAsync".
Now, there are few options and
parameters I can pass in such as a string,
a URI, and this
BrowserLaunchMode which I'll talk about here in a second.
So, let's just go ahead and open up
xamarin.com and then we can give it a BrowserLaunchMode.
Now, here we're going to see that
the browser launch mode is either external or system preferred.
This is optional and by default,
it will use system preferred.
The external launcher means that it's going to open
the browser outside of your application,
so it's completely launching a new activity outside.
Whereas system preferred, if available,
will use the optimized browsers such
as Chrome Custom Tabs and SF Safari View Controller.
This allows you and your user to stay inside of
your application and it gives
a more delightful experience. So, here we go.
I'll just go ahead and say, "Add System Preferred."
Now, this is an async task and this will
fire off in a way to ensure that the browser has been launched.
So, I'll go ahead and await on that and will mark
this as async. There we go.
So, now when I go ahead and run my application again,
what we're going to see is that single line of code,
it will automatically launch my Browser.
So, let's go ahead and let it recompile, and here we go.
The app has relaunched and I'll have to do is tap on "Forgot
Password" and just like that, it opens up.
I can see now everything I need to know about Xamarin,
and we can notice here that when I tap on the top right
"Triple.menu" we can see that this is powered by Chrome
and I can even launch Chrome from this part.
But the nice thing here is that if I hit the "X",
I'm immediately back into my application.
User doesn't have to switch contexts and that's it.
With literally a single line of code,
you're launching your browser with optimized settings such as
Chrome Custom Tabs by ran that on
iOS, SFSafariViewController as well.
You can find all the links for the documentation
in this sample application in the show notes below
and check out all of the awesome Xamarin Essential API videos
of the Week. Thanks for watching.
[MUSIC]
>> Hey. It's James. I'm just
checking in and wanted to thank you for
watching this awesome Xamarin Essential API the Week,
and don't forget to do all the things such as like,
subscribe, and ding that notification bell for even more videos.
While you're here check out
more Xamarin Essential API videos of the week.
They're all right here. Go do it right now. It's Awesome.