Kmm-Permissions is a Kotlin Multiplatform library designed to simplify permission handling in Android and iOS applications. By providing a unified API, it enables developers to manage permissions seamlessly across both platforms.
-
Add the KMM Permissions dependency to your project's
build.gradle.ktsfile.commonMain.dependencies { implementation("tech.kotlinlang:permission:<version>") } -
Add below permission in
AndroidManifest.xmlfor Android.<!-- For location permission--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- For notification permission--> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- For camera permission--> <uses-permission android:name="android.permission.CAMERA" />
-
Add below message in
info.plistfor iOS.<!-- For location permission--> <key>NSLocationWhenInUseUsageDescription</key> <string>Allow $(PRODUCT_NAME) to determine your location</string> <!-- For camera permission--> <key>NSCameraUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your camera</string>
-
Add below in
MainActivityforandroidMainto initialize permission manager in Android.override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) PermissionInitiation.setActivity(this) // Initialize here setContent { // ... } }
-
Use
getPermissionHelperextension function fromcommonMainwhich helps to create new instance for permission helper class.// Permission Helper to request permission val permissionHelper = HelperHolder.getPermissionHelperInstance() // Location Helper to request permission to fetch location val locationHelper = HelperHolder.getLocationHelperInstance()
-
Use
permissionHelperinstance to check permission state or request permission. Both functions are suspended, useDispatcher.Mainfor access permissions. Also you can uselocationHelperto fetch location updates.// Use permission object, Permission.Location, Permission.Notification, Permission.Camera val permission = Permission.Location val checkPermissionResult = permissionHelper.checkIsPermissionGranted(permission) // Request Permission val requestPermissionResult = permissionHelper.requestForPermission(permission) // Fetch Last known location val locationRequestResult = locationHelper.fetchLastKnownLocation()
| S. No. | Location Permission Result | Description |
|---|---|---|
| 1 | LocationPermissionResult.Denied | Location Permission is Denied by user. But developer can request again for permission. |
| 2 | LocationPermissionResult.NotAllowed | Location Permission is Denied by user. Developer cannot request again for permission. Developer have to navigate user to settings page. |
| 3 | LocationPermissionResult.Granted.Precise | Precise Location Permission is Granted by user |
| 4 | LocationPermissionResult.Granted.Approximate | Approximate Location Permission is Granted by user |
| S. No. | Notification Permission Result | Description |
|---|---|---|
| 1 | NotificationPermissionResult.Denied | Notification Permission is Denied by user. But developer can request again for permission. |
| 2 | NotificationPermissionResult.NotAllowed | Notification Permission is Denied by user. Developer cannot request again for permission. Developer have to navigate user to settings page. |
| 3 | NotificationPermissionResult.Granted | Notification Permission is Granted by user. Now notification can be send to device. |
| S. No. | Camera Permission Result | Description |
|---|---|---|
| 1 | CameraPermissionResult.Denied | Camera Permission is Denied by user. But developer can request again for permission. |
| 2 | CameraPermissionResult.NotAllowed | Camera Permission is Denied by user. Developer cannot request again for permission. Developer have to navigate user to settings page. |
| 3 | CameraPermissionResult.Granted | Camera Permission is Granted by user. Now Developer can access camera. |
| S. No. | Location Request Result | Description |
|---|---|---|
| 1 | LocationRequestResult.LocationData | Provides you latitude and longitude for you current location |
| 2 | LocationRequestResult.PermissionFailure | This returns when location permission is not granted location |
| 3 | CameraPermissionResult.Granted | This returns when device is not fetched location from very long time or any failure occur |