
Enables shared code structure for Compose Multiplatform applications, organizing platform-specific code and facilitating integration of native elements like Apple's CoreCrypto and SwiftUI for iOS projects.
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.kts file.
commonMain.dependencies {
implementation("tech.kotlinlang:permission:<version>")
}Add below permission in AndroidManifest.xml for 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.plist for 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 MainActivity for androidMain to initialize permission manager in Android.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PermissionInitiation.setActivity(this) // Initialize here
setContent {
// ...
}
}Use getPermissionHelper extension function from commonMain which 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 permissionHelper instance to check permission state or request permission. Both functions are suspended, use Dispatcher.Main for access permissions. Also you can use locationHelper to 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 |
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.kts file.
commonMain.dependencies {
implementation("tech.kotlinlang:permission:<version>")
}Add below permission in AndroidManifest.xml for 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.plist for 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 MainActivity for androidMain to initialize permission manager in Android.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PermissionInitiation.setActivity(this) // Initialize here
setContent {
// ...
}
}Use getPermissionHelper extension function from commonMain which 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 permissionHelper instance to check permission state or request permission. Both functions are suspended, use Dispatcher.Main for access permissions. Also you can use locationHelper to 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 |