CMPEasyPermission: A Compose Multiplatform Permission Library for Android and iOS
In the realm of mobile app development, managing permissions across different platforms can be a daunting task. CMPEasyPermission simplifies this process by providing a unified permission library for Compose Multiplatform, supporting both Android and iOS. This article will guide you through the installation and usage of CMPEasyPermission, making your development experience smoother and more efficient.
Installation
To get started with CMPEasyPermission, add the following dependency to your build.gradle.kts
file:
commonMain.dependencies {
implementation("network.chaintech:cmp-easy-permission:1.0.1")
}
Usage
Android
For Android, you need to declare the necessary permissions and features in your AndroidManifest.xml
file:
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.CAMERA"/>
iOS
For iOS, add the following key to the Info.plist
file in your Xcode project:
<key>NSCameraUsageDescription</key><string>$(PRODUCT_NAME) camera description.</string>
Example
Here’s an example of how to use CMPEasyPermission in your Compose Multiplatform project:
var openCamera by remember { mutableStateOf(false) }
var isGrantedCamera by remember { mutableStateOf<Boolean?>(null) }
if (openCamera) {
RequestPermission(
permission = PermissionState.CAMERA,
openSetting = true,
deniedDialogParams = DialogParams(
titleStr = "App requires access to your camera",
messageStr = "You can enable this permission in the settings",
),
isGranted = { isGranted ->
isGrantedCamera = isGranted
openCamera = false
},
)
}
Box(
modifier = Modifier
.clickable {
openCamera = true
}
) {
// your content
}
Explanation
- permission: Represents the requested permission.
- openSetting: A flag indicating whether to open the device settings if the permission is denied.
- deniedDialogTitle: The title text for the dialog that appears if the permission is denied.
- deniedDialogDesc: The description text for the dialog that appears if the permission is denied.
- isGranted: A callback function that is invoked with a boolean value indicating whether the permission was granted or not.
Demo
Github link👇
Connect with us 👇