Today, let's talk about permissions and .NET Maui applications.
Yes.
Those little popups that you need to use to request different permissions
on different operating systems.
When you're developing applications today, I'm gonna talk about everything
you need to know about the built in permission systems of .NET MAUI, but
lets you handle all sorts of different permissions, but also how to create your
own custom permissions for circumstances, you may get yourself into when you are
needing to access different permissions on all the different operating systems.
So tune in.
Hi everyone.
I'm James.
And then today we are talking about permissions.
That's right.
Permissions.
Not the permissions plug-in that I have or the one that turned into
the zer and essentials permissions.
No, no, no.
The one that is built into.net Maui permissions are a very
fascinating part of the system.
There are sort of permissions that you need to set, like on Android
that are just like invisible.
Like, Hey, I need network access.
But there are other ones on all the different operating systems that you
need to set and actually need to request those permissions from your users.
You need to get that pop up.
You need to get access to them.
Each operating system handles it a little bit different on Android.
Uh, you kind of get a.
Few different chances to do the permission pop up so you can pop up a dialogue
and request, deny, request, deny you can actually like get some insight.
Like if they've denied it a few times with, with like, should
show rationale an iOS on the other hand, it's a little bit tricky.
You get one chance, no matter what, and that's it.
Like you get one.
Permission prompt and that's all ever.
So you'd have to see if it's denied and then tell them to go into
settings and turn it on manually.
And the same thing on windows.
You only get like a few chances there too.
And of course, same thing on Mac.
You only get one opportunity cuz it's Mac catalyst.
So you need to kinda understand how the different operating systems work, but
you don't need to know exactly how the implementation of each operating system
needs to be done because .NET MAUI has a built in permissions API to handle
things, completely cross platform, which.
Now it has a bunch of different permissions out there.
Cause you know that every single operating system has different one, but things
like location and network connectivity, um, Things like photos and camera and,
uh, um, accessing the file storage.
Like that's all built in automatically.
And today I'm gonna show you how to use those APIs, but also when you need to
consider custom permissions as well.
So for example, I was recently upgrading my application, my cadence,
which speaks to Bluetooth devices.
Now on iOS, when you access Bluetooth APIs, it gives you the popup and
permission automatically, um, which is quite nice, but on Android,
it's very, very complicated.
In fact, on Android 11 or API, 30 or lower, you needed to request a location
permission, which is super weird.
But then in Android, 12 API 31, uh, you were able to actually have a new
Bluetooth permission that's actually built into, which is quite nice.
There's different sort of process.
You need to go down there.
So let's first talk about the built-in permissions and how we would handle
this on Android or iOS or other things, and then how to do a custom permission.
So let's get into it.
All right.
Let's talk about those built in permissions.
There's tons of permissions built in to .NET MAUI.
You can read all about it in the documentation that I
will go ahead and link to.
Now we are gonna go ahead and implement a Bluetooth permission
specifically for Android.
Now, the first thing I need to do is handle the older location setting.
So just location cuz that's what it was required on Android back in the day.
And those are all built into Don ma, which is nice.
Now the custom Bluetooth permissions specifically for Android aren't.
So we're gonna use the custom permissions later on the video.
So here I've created a relay command.
That's gonna say request Bluetooth.
Now what's cool about that is that this is built into the, then VVM toolkit.
So I can go ahead and, you know, generate some source code and it gives
me a Bluetooth request right here.
And I'll put a video up over there and all the things that
you need to know, uh, in general.
Okay.
And I'm only gonna show that on Android since we're just using that
this example, but all of the built in permissions based on what that
grid you just saw will be supported.
So now let's go ahead and take a look what we're gonna do.
We're gonna use this permission status, which is something that's
actually built into Don Maui.
So Microsoft Maui application model, there's a permission status.
So there's all sorts of different ones.
Like granted disabled denied, restricted limited.
Now, um, by default, it's gonna be either unknown or denied, and we're gonna need to
check that and then do a check or request.
So, what I can do is go in and say permissions dot.
And there's a bunch of these different, um, classes that
are inside of permissions.
And these are all of the different permissions available things like
battery, calendar, camera, flashlight, location, maps, media network,
state, phone, photo reminders, SMS.
Some of these are gonna require.
Popups and some of them aren't.
So just kind of know, based on the platform, some that may be, you know,
popups, but some of them might be, uh, if you're a library creator, you wanna
use this as a core to see if the, if we've requested or put in permissions.
Now, additionally, there are some methods such as check status.
Ay.
Should show rationale async, which is only an Android specific API, but
that basically means that the user has denied at least once or revoked.
And you should give them some more information as to
why you're requesting it.
And then of course, request AYQ.
So let's go ahead and first do this.
We'll say status equals a wait, permissions dot check status.
Ay.
Now this check permit, check status, ay takes in a T
permission or a base permission.
So I'm gonna say permissions dot location, win in use, just like that.
That will gimme back some information.
So I can say if status.
Equals equals permission, status dot.
Granted, let me just go ahead and return else since I'm on Android.
And I probably want to be a good citizen.
I'll say if permissions dot, um, re what is it should show rationale again.
Permissions dot location, win in use there.
All right.
And here, what we would do is probably do like a popup.
So I'd say a weight, shell dot current dot main or display.
There you go, display alert, and I'll say needs permission.
Uh, here, you should give a really good reason, you know, inside
here because, uh, you need it.
And then, uh, okay, there we go.
And I'm not gonna return because we actually need to
request the permission now.
So you guess that we're gonna go ahead and say status equal.
Uh, permissions dot request.
Async I'm gonna say permissions dot location when in use.
So we go and again, we're gonna wait on that as well.
There we go.
Perfect.
So you guys it check.
Should we show rationale and then actually request it.
So let's go ahead and see all the statuses as we run this application
and I'm specifically gonna build it up and compile it on my Android emulator.
Now, the only other thing I've done is I've gone into my Android manifest file
and I've added all the permissions.
I need not only for like Bluetooth stuff, but also these access course
location and fine location, which again are required on those older.
In the future here on Android 12, we're gonna use these new Bluetooth scan and
Bluetooth connect permissions that we'll see in a few, um, minutes here when
we create our own custom permission.
And then down here, I'm gonna set this uses feature, which is specific to
Android to say only do this, um, on the app store for devices that have B.
For setting permissions on iOS or Mac or windows, you wanna go into
your info P list or package manifest files and set those up there as well.
All right, now this is gonna be running on my actual Android device.
So let's give this a, uh, few minutes to go ahead and compile up and start running.
All right.
Our application has been loaded up.
There's our splash screen, and this is a simple application I made to
show dependency injection earlier.
Uh, so it has a bunch of buttons like to navigate and do dependency injection.
It has this request, Bluetooth button.
So let's go ahead and tap on it.
And now we can see the first thing is that the permission has been denied.
And that's the default state on Android.
I believe on iOS it's unknown.
So again, there's differences based on the platform because permissions are wacky.
Let's continue.
Should I show the rationale?
Well, no, because you know, in general, um, I haven't requested it yet, but
let's go ahead and request it now.
And just like that, we get a permission pop up, asking us what we want to do.
So here, while using the app or only this time.
So again, it's gonna be a little bit different based on the platform.
If I select only this time, the next time I launch the
application, it won't be granted.
It'll be denied again.
But I'm gonna say while using this.
Our status here is granted and I can continue on using my application.
There we go.
Now, if I go ahead and tap on this again, what we're gonna go ahead
and see is that the permission has been granted automatically for me.
There we go.
So now just like that, nothing else is gonna pop up.
No permissions, no checks or anything like that.
All right there, we have it simple, simple way of implementing permissions
on the different operating systems.
That exact same source code will work on iOS and on windows and Mac catalyst.
Now, of course, again, you are gonna get different, you know, permission
prompts in different areas where you're gonna need to add that information
such as the info P list, for example.
So make sure you go ahead and read documentation when
you wanna access those API.
Okay.
What about custom permissions?
Because I need to handle that now on Android for Bluetooth.
So let's check that out.
Okay.
We mentioned earlier here that we have these two permissions over here, so we
can create our own custom permission that we can use inside of our application.
So I'm gonna go in into my helper's folder and I'm gonna say.
New class.
So just anywhere inside of my .NET MAUI application, I'm gonna
say my Bluetooth permission.
There we go.
Now let's go ahead and clean this up a little bit.
There we go.
Let's remove all this stuff.
Perfect.
And we have a Bluetooth permission.
Now, if you remember everything inherited from base permission.
And in fact, if I go over here and I right click on location, when in use and
go to definition, we can see that inside of this class, we have all of these
permissions, this required permissions.
It's doing a bunch of stuff inside of here.
Basically there's implementation here on the different platforms.
So I could inherit from base permission.
If I wanted to do something completely custom.
So let's go ahead and check that out.
So I'm gonna say base permission.
There we go.
And bring in the name space right there.
Cool.
Now, when I do base permission, that's an abstract class.
Now what that means I'm gonna create my own permission and I need to implement
all four methods that are required to do the checking, the prompting, ensuring
that it's been declared correctly.
So here, this is really cool because while I'm gonna use this for Bluetooth,
you could actually have this set up in your obligation to do something like,
you know, um, Um, my admin permission, you know, my, you know, regular user,
my, you know, super user account.
So you could, you could use this as your own permission system.
It doesn't have to be a prompt.
You know, this is something that's built in.
These are methods that are automatically there for you.
So you can do check status.
You can see what the, the users are logged in.
If they're doing stuff you could do all this automatically.
Now, I mentioned though that this.
Location went in use inherits, not only from base permission,
but from base platform permission.
So here we can see that a base platform permission is a permission
for the specific platform.
So that actually really, really neat.
So base permission enables us to create our own permission, handle everything.
Tap into the permission system, but we could say base platform permission here.
And when we say base platform permission, we can still override those individual
methods that are inside of it.
Or we can tap in and override platform specific capabilities.
So here I have the ability to override check status, do
check ensure declared there's.
This required permissions should show rationale.
There's a bunch of these things.
So base platform permission is the Android iOS Mac or windows implementation.
So you're not gonna have this available if you're accessing this in sort of like a.
You know, done at standard or done SX library, but if you're inside your Maui
application, this is gonna work great.
So what I'm gonna do is I'm gonna say, let me override the required permissions.
So, what we wanna do is say, Hey, listen, .NET MAUI is
already doing all the hard work.
It knows how to prompt on Android tie into the different systems.
What if I just tell it different permissions to use, for example?
Well, I can do that.
So what I can do is I can say let's create a new list of string, um,
permission and bull is a run time.
And then this requires an array.
I'm just gonna say two array, just like that.
And there we have it.
Now I need to give it the two different permissions that I need.
And I'm just gonna ahead and copy and paste that in here.
Um, to make it just a lot easier in general.
Now, I guess I need to do this two pull, correct as a, well, there we go.
Perfect.
So I'm gonna give it a two pull of permission and bull.
So if you take a look at the actual source code implementation for.
These different permissions.
We can actually tap over here and say, Maui source essentials source.
You could tap in to permissions, which is gonna be right here.
In Android.
And then you could go ahead and take a look at how these were implemented, right?
So we could see that here's base platform permission.
We can see all the status checks, all the source code and really
all that was really required.
If we go down further here for battery, for example, is that it's gonna take in.
An override.
It can also check if the different things, um, are, are, are implemented or not.
If it's in the manifest, but for calendar, it simply overrides that.
So that's what we're gonna do.
We're gonna override that one method and we're done.
Okay.
Well almost now.
That's an Android specific override.
So if I toggle this over to iOS, we're gonna see that now required permissions.
Aren't there.
If I hit override, we're gonna see have other things like
required info blist keys.
Oh my goodness.
So there's two ways that we can handle this one.
We could put this into our platform folder into our Android here.
We could also create, you know, an actual interface to.
Bluetooth permission check and then implement it on the different platforms
or we can conditionally compile.
So I can say if Android and do, and if.
And that's it.
Now that API's only gonna be exposed on Android now we're totally
good to go, which is awesome.
So that's one way of doing it.
You can also create partial classes and you can make sure the name space is
aligned and you can have a partial class inside of your Android and then up here.
Okay.
But this is what I'm gonna do.
Cause I think this is much more elegant, uh, to just simply put
this in here for this one use.
Now next up, I'm gonna need to go ahead and check these permissions.
So this is actually kind of cool.
The first thing we're gonna do is we're gonna go in and say, uh, if device
info dot platform does not equal device platform.android, then we're gonna return.
Cause I'm only doing this on Android.
And as we mentioned earlier, I'm only gonna show this on Android.
Anyways, the next thing I'm gonna do over here is I'm gonna say.
If device info dot version dot major is greater than or equal
to 12, because again, on Android 12 and above that specifically is
where the new API needs to be used.
So I can go ahead and do this.
I'll do else.
There we go.
And I can go ahead and copy this into this.
I can also now copy that exact same thing into this block, but instead
of using permissions, that location went in use you'd guess it I'm
gonna use my Bluetooth permission, just like this, which is very cool.
Boom.
Now it's gonna use my new Bluetooth permission, which is super rad.
So now what I can do is add some break points on top of here.
Let's add the break points here.
Let's add one more down here.
And then what I'm gonna do is I'm gonna go ahead and toggle this over
to my actual pixel four device.
So we can see it running on my actual pixel phone, which is kind of cool.
And again, we're gonna, let's add another break point here for the version so
we can make sure that that's actually running only on this specific source code.
So let's give this a few seconds to compile up and get.
Okay.
My application is running.
I'm actually gonna use visor here to screen mirror because the
permission prompt, isn't gonna be visible inside of this here.
So let's go ahead and close this down.
Now I'm gonna hit the request Bluetooth, and we're gonna see that
our version over here is version 12.
So we're totally good to go.
Now let's go ahead and check the permission and we can see that is denied.
That's the default state let's go ahead and run through and sure.
We get our new permission.
That's asking about relative position of nearby devices, AKA Bluetooth devices.
And we can simply say allow or don't.
Just like that.
And now we can see that our status is granted right in our source code.
And just like that we've implemented custom permissions and used the
default permissions in .NET MAUI.
It's a really great way of extending the permission system and overriding
and using that base implementation.
If you need to, you can, of course, tap into iOS and Android different resources
there, which I think is really cool.
And of course you can always create your own interface and,
and, you know, do different things.
You're like I Bluetooth permission handle it separately, totally up
to you, whatever you wanna do.
But I think that conditional compilation in this case, it
makes it very, very easy to do.
Hopefully you found this video not only helpful when just doing
normal permissions, but also if you run into that issue and sort.
Not even an issue, but circumstance where custom permissions are needed.
Now, you know how to do it.
I'll link to all the documentation below.
And of course this sample as well, showing you exactly how to do it.
Hope you found this video helpful.
If you did give it a thumbs up.
And of course, don't forget to subscribe Ning that ding, that notification bell.
So you can notify it every time and put out a video right here on YouTube.
All right, that's gonna do it for this time.
Feel free to leave questions and comments below.
I really appreciate it.
Have a good one.