Git Product home page Git Product logo

permissionsplugin's Introduction

Permissions Plugin for Xamarin

Simple cross platform plugin to request and check permissions.

Want to read about the creation, checkout my in-depth blog post.

Setup

Platform Support

Platform Version
Xamarin.iOS iOS 8+
Xamarin.Android API 14+
Windows 10 UWP(Beta) 10+

*See platform notes below

Build Status: Build status

Android specific in your BaseActivity or MainActivity (for Xamarin.Forms) add this code:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

Android Current Activity Setup

This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity's OnCreate method:

Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);

It is highly recommended that you use a custom Application that are outlined in the Current Activity Plugin Documentation](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md)

iOS Specific

Based on what permissions you are using, you must add information into your info.plist. Please read the Working with Security and Privacy guide for keys you will need to add.

API Usage

You are able to check and requests permissions with just a few lines of code:

Check permission:

PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync<CalendarPermission>();

Request permission:

PermissionStatus status = await CrossPermissions.Current.RequestPermissionAsync<CalendarPermission>();

Additionally on Android there is a situation where you may want to detect if the user has already declined the permission and you should show your own pop up:

bool shouldShow = await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Calendar);

Available Permissions

  • CalendarPermission
  • CameraPermission
  • ContactsPermission
  • LocationPermission
  • LocationAlwaysPermission
  • LocationWhenInUsePermission
  • MediaLibraryPermission
  • MicrophonePermission
  • PhonePermission
  • PhotosPermission
  • RemindersPermission
  • SensorsPermission
  • SmsPermission
  • StoragePermission
  • SpeechPermission

In Action

Here is how you may use it with geolocation:

try
{
	var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
	if (status != PermissionStatus.Granted)
	{
		if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
		{
			await DisplayAlert("Need location", "Gunna need that location", "OK");
		}

		status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
	}

	if (status == PermissionStatus.Granted)
	{
		//Query permission
	}
	else if (status != PermissionStatus.Unknown)
	{
		//location denied
	}
}
catch (Exception ex)
{
  //Something went wrong
}

Read more about android permissions: http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

IMPORTANT

Android:

You still need to request the permissions in your AndroidManifest.xml. Also ensure your MainApplication.cs was setup correctly from the CurrentActivity Plugin.

Windows 10 UWP

UWP has a limited set of supported permissions. You can see the documentation above, but current support: Contacts, Location, and Sensors.

Contributors

Thanks!

License

Licensed under main repo license(MIT)

Want To Support This Project?

All I have ever asked is to be active by submitting bugs, features, and sending those pull requests down! Want to go further? Make sure to subscribe to my weekly development podcast Merge Conflict, where I talk all about awesome Xamarin goodies and you can optionally support the show by becoming a supporter on Patreon.

permissionsplugin's People

Contributors

jamesmontemagno avatar aritchie avatar hvaughan3 avatar prashantvc avatar jcmanke avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.