>> On this week's Essential API of the Week,
I introduce you to Connectivity to you guessed it,
check connectivity in your mobile apps. So, tune in.
[MUSIC] Welcome back everyone to the Xamarin show,
Essential API of the Week.
Today, I'm going to show you how to use
the Connectivity API from
Xamarin Essentials to check connectivity.
Now, let's just head right over to the code and get started.
The first place I get started every single time is
in the Xamarin Essentials documentation.
Here, I can read how to get started and all of the feature guides.
So, I've opened up the "Getting Started" guide in Connectivity.
So, the first thing that we want to do is ensure that we have
the Xamarin Essentials NuGet package
installed into all of our projects;
iOS, Android and Windows 10 and into our.NET standard library.
Now, the gray part here is that
Xamarin Essentials works with any iOS Android
or Windows app regardless if you're using
Xamarin Forms to build your user interface.
Finally, there's a few little bits of
setup code that you have to write in your Android application.
So, make sure you follow this.
Now let's go ahead and start using
the Xamarin Essentials API for connectivity.
Now, every single API for Xamarin Essential has
a short "Getting Started" guide that will
outline any additional setup you may need.
So, here, for connectivity for Android,
we need to ensure that we add this using permission
into our assembly info or our Android Manifest.
For iOS and Windows 10,
no additional setup required.
So, let's take a look at our project real quick.
This is a quick simple login application that I've been
building up called my "AppEssentials"
and I'll continue to add to this over time.
So, the first thing is that here I
have all my NuGet packages including
Xamarin Essentials installed into
my iOS Android and.NET Standard library.
Inside of my Android project is my assembly
info.cs and I've added that access and network state right there.
So, now we want to start using that API.
So, if I scroll down a little bit further,
we want to make sure we add a using statement.
If I want to check for internet access, it's right here.
Then, I can learn about other things
such as checking for connectivity,
change events or other limitations that it may have.
So, let's take a look at the app first.
Here, I have a simple login.
So, I can say "James" I could say "Hello
World" and it'll show me if I have a valid state.
It's not checking to see if I'm actually online. So, here I am.
So, that's okay but let's add
some additional validation using Xamarin Essentials.
So, if I look at my login page,
I have a few entries
and then a label and a button that I can click on.
Whenever that button is clicked,
I get a handle clicked event in the code behind.
So, let's take a look there.
When I come in here,
we'll see it's just validating the username and
password field and just going off and trying to log in.
So, the first thing that we're going to want to do is add
a using statement to xamarin.essentials. There we go.
Next, down here before we do any validation,
let's check to see if we're online.
So, let's zoom in here.
Now if I was to come in and say "if Xamarin.essentials."
I would get access to every single API in
Xamarin Essentials including connectivity.
Then I can add additional things like here's the network access.
I can say if I'm not online.
So, Internet access.
Let's go ahead and do something.
Now, since I've added the Xamarin Essentials namespace already,
we can go ahead and simply remove that namespace from there.
So, return if we're not online and I'll just say
"DisplayAlert" and I'll say "No
Internet" and we'll say "Okay" There we go.
So, now, if we go ahead and run this application,
we'll bring in Xamarin Essentials.
We've added the permission for Android and we've added
just one line of code to check for internet access.
Xamarin Essentials we use the native APIs for each platform to
ensure that you're getting a great native functionality.
So, I'm online.
So, if I go ahead and try to login
so "James" I'll say "Hello World."
Say "Log In." We're online, we're good to go.
Now, let's say I put this into airplane mode.
Immediately if I hit "Log In" again,
I see no internet because Xamarin Essential
has picked up that I'm not online. Guess what?
When I come back online,
here we'll see I get
"LTE" and then I can get "WiFi", "Log In" successful.
I'm good to go again.
Let's take it a step further.
Perhaps, I want to display a little information to
my user to say I'm disconnected or I am connected.
So, here I have a label at the bottom that says
"Disconnected" with the opacity set to zero.
So, instead of on the click,
let's go ahead and add a change event for
connectivity when this page appears and disappears.
So, again, I'll say "Connectivity."and
here I have "ConnectivityChanged".
So, add an event inline.
Best practic is of course to unregister that event.
So, I'll say "Connectivity changed
minus equals "ConnectivityChanged" event arguments
so that way we're unregistering our event.
Now whenever this appears we want to do a few things.
So, I'll just go ahead and paste some code in here.
If I'm online, I want to fade that to zero.
But if I'm not online,
let's fade in that label to one.
So, we get a nice little animation here.
So, let's go ahead and rerun
this application now I'm online already.
So, the next time it launches,
it shouldn't get any event changed or
any arguments that we need and it's already at zero.
There we go. So, now if I come
in and I say I'm offline, it fades in.
Now I tap it again, it fades out.
Now, the best part here is I can run that same exact code on
iOS or UWP and get the same great native functionality.
So, there you have it.
In just a few seconds,
we've integrated connectivity into
our application with Xamarin Essentials.
Thanks for tuning in and make sure you subscribe for
the next Essential API of
the Week right here on YouTube or Channel nine.
[MUSIC]