XToolkit Permissions
Extended library over the Xamarin.Essentials Permissions that covered common cases of working with permissions.
Install
When you use this component separately from WhiteLabel.
Install-Package Softeq.XToolkit.Permissions
Setup
Android
Register dependencies in platform-specific Bootstrapper:
// permissions
builder.Singleton<PermissionsService, IPermissionsService>();
builder.Singleton<PermissionsManager, IPermissionsManager>();
builder.Singleton<RequestResultHandler, IPermissionRequestHandler>();
Add Handler
IMPORTANT
This section only for WhiteLabel.Forms.Droid project.
WhiteLabel provides this functionality by default.
Add registrations to MainActivity
(Forms.Droid entry point):
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// ...
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// ...
}
public override void OnRequestPermissionsResult(
int requestCode,
string[] permissions,
[GeneratedEnum] Permission[] grantResults)
{
Dependencies.Container.Resolve<IPermissionRequestHandler>()?.Handle(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
iOS
Register dependencies in platform-specific Bootstrapper:
// permissions
builder.Singleton<PermissionsService, IPermissionsService>();
builder.Singleton<PermissionsManager, IPermissionsManager>();
Declare permissions
Don't forget to add permission declarations:
- iOS: Info.plist
- Android: AndroidManifest.xml
Using
Get IPermissionsManager
from the constructor:
public class NewPageViewModel : ViewModelBase
{
public NewPageViewModel(IPermissionsManager permissionsManager)
{
// ...
}
}
Check Permission with Request
await _permissionsManager.CheckWithRequestAsync<Xamarin.Essentials.Permissions.Camera>();
Check Permission Only
await _permissionsManager.CheckAsync<Xamarin.Essentials.Permissions.Camera>();
Description
Common Contracts
Contract | Implementation |
---|---|
IPermissionsManager | iOS, Android |
IPermissionsService | iOS, Android |
IPermissionRequestHandler | Android |
PermissionStatus | Core |
Default Platform-Specific implementation
Default implementation handles a set of predefined permissions (see below) and has additional behavior to request permissions and double-dialog check and opening application settings when the permissions are Denied.
Default implementation depends on Xamarin.Essentials.Permissions