Check and Request Permissions in .NET MAUI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today let's talk about permissions and Don Maui applications yes those little pop-ups that you need to use to request different permissions on the different operating systems when you're developing applications today I'm going to talk about everything you need to know about the built-in permission systems of Don and Maui 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 [Music] foreign [Music] I'm James and today we are talking about permissions that's right permissions not the permissions plugin that I have or the one that turned in to the xamarin essentials permissions I don't know the one that is built in to.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 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 an eye you can actually like get some insight like if they've denied it a few times with with like should show rationale on 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 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 because it's Mac catalyst so you need to kind of 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 Don and Maui has a built-in permissions API to handle things completely cross-platform which is nice now it has a bunch of different permissions out there so you know that every single operating system has different one but things like location and network connectivity things like photos and camera and accessing the file storage like that's all built in automatically and today I'm going to 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 pop-up and permission automatically which is quite nice but on Android it's very very complicated in fact on Android 11 or API AI 30 or lower you needed to request a location permission which is super weird but then in Android 12 API 31 you were able to actually have a new Bluetooth permission that's actually built into it which is quite nice so 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 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're going to go ahead and Implement a Bluetooth permission specifically for Android now the first thing I need to do is handle the older location settings so just location because that's what was required on Android back in the day and those are all built into Dynamo which is nice now the custom Bluetooth permission specifically for Android aren't so we're going to use the custom permissions later on the video so here I've created a relay command that's going to say request Bluetooth now what's cool about that is that this is built into the mvvm 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 cool and I'm only going to show that on Android since we're just using that in 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 going to do we're going to use this permission status which is something that's actually built into dot Maui so Microsoft Maui application model there's a permission status so there's all sorts of different ones like Granite disabled denied restricted Limited Now by default it's going to be either unknown or denied and we're going to 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 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 going to require pop-ups and some of them aren't so just kind of know based on the platform some that maybe you know pop-ups but some of them might be if you're a library Creator you want to 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 async 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 async so let's go ahead and first do this we'll say status equals await permissions dot check status async now this check permatic check status async takes in a t permission or a base permission so I'm going to say permissions dot location when in use just like that that will give me 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 here what is it should show rationale again permissions dot location when in use there all right and here what we're going to do is probably do like a pop-up so I'd say a weight shell dot current dot me or display there you go display alert I'll say needs permission uh here you should give a really good reason you know inside of here because uh you need it and then uh okay there we go and I'm not going to return because we actually need to request the permission now so you guessed it we're going to go ahead and say status equals uh permissions Dot request async I'm going to say permissions dot location when in use so here we go again we're going to wait on that as well there we go perfect so you guess 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 going to 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 find location which again are required on those older devices in the future here on Android 12 we're going to use these new Bluetooth scan and Bluetooth connect permissions that we'll see in a few minutes here when we create our own custom permission and then down here I'm going to set this uses feature which is specific to Android to say only do this on the app store for devices that have Bluetooth for setting permissions on iOS or Mac or Windows you want to go into your info p list or package manifest files and set those up there as well all right now this is going to be running on my actual Android device so let's give this a 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 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 going to 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 going to say while using this app 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 going to 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 going to 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 going to get different you know permission prompts in different areas we're going to need to add that information such as the info p list for example so make sure you go and read documentation when you want to access those apis 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 going to go in into my helpers folder and I'm going to say add new class so just anywhere inside of my.maui application I'm going to 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 remission 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 going to say base permission there we go and bring in the namespace right there cool now when I do base permission that's an abstract class now what that means I'm going to 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 well I'm going to use this for Bluetooth you could actually have this set up in your application 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 see what to use the user's logged in if they're doing stuff you could do all this automatically now I mentioned though that this location when 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 those are really really neat so base permission enables us to create our own permission handle everything tap in to 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 going to have this available if you're accessing this in sort of like a different you know done it standard or done SX library but if you're inside your Maui application this is going to work great so what I'm going to do is I'm going to say let me override the required permissions so what we want to do is say Hey listen Donna 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 permission and bull is a runtime here and then this requires an array so I'm just going to say two array just like that and there we have it now I need to give it the two different permissions that I need I'm just going ahead and copy and paste that in here to make it just a lot easier in general now I guess I need to do this Tuple correct as well there we go perfect so I'm going to give it a tuple 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 going to 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 going to take in an override it can also check if the different things are are implemented or not if it's in the Manifest but for calendar it simply overrides that so that's what we're going to do we're going to 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 going to see that now require permissions aren't there if I hit override we're going to see I have other things like required info PLS 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 as a 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 is only going to be exposed on Android and 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 namespace is aligned you can have a partial class inside of your Android and then up here okay but this is what I'm going to do because I think this is much more elegant to just simply put this in here for this one use case now next up I'm going to need to go ahead and check these permissions so this is actually kind of cool the first thing we're going to do is we're going to go in and say if device info dot platform does not equal device platform dot Android then we're going to return because I'm only doing this on Android and as we mentioned earlier I'm only going to show this on Android anyways the next thing I'm going to do over here is I'm going to 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 block I can also now copy that exact same thing into this block but instead of using permissions.location when in use you'd guess it I'm going to use my Bluetooth permission just like this which is very cool boom now it's going to 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 the break points here let's add one more down here too and then what I'm going to do is I'm going to go ahead and toggle this over to my actual pixel 4 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 running okay my application is running I'm actually going to use visor here to screen mirror because the permission prompt isn't going to be visible inside of this here so let's go ahead and close this down now I'm going to hit the request Bluetooth and we're going to 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 as denied that's the default State let's go ahead and run through and sure enough 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 allow 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 Don 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 you know do different things or like I Bluetooth permission handle it separately totally up to you whatever you want to do but I think that conditional compilation in this case it makes it very very easy to do hope you found this video not only helpful when just doing normal permissions but also if you run into that issue and sort of not even 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 I 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're notified every time put out a video right here on YouTube all right that's going to do it for this time feel free to leave questions and comments below I really appreciate it have a good one [Music]
Info
Channel: James Montemagno
Views: 16,463
Rating: undefined out of 5
Keywords: .net maui, dotnet maui, .net 6, .net maui tutorial, .net maui getting started, .net maui essentials, dotnet maui permissions, geolocation permission, bluetooth permission, .net maui bluetooth, .net maui community, .net 6 xamarin, visual studio 2022, .net maui desktop, .net maui app, .net, dotnet maui tutorial, requesting permissions, .net maui permissions
Id: 9GljgwfpiiE
Channel Id: undefined
Length: 19min 47sec (1187 seconds)
Published: Thu Sep 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.