AFPermissions

actual object AFPermissions

Provides functionality for checking and requesting runtime permissions on Android.

This object manages permission requests by associating them with a PermissionHost, which is typically tied to a ComponentActivity.

Important: Before requesting any permissions, you must call AFPermissions.getOrCreateHost(activity) during the onCreate() lifecycle method of your Activity or Fragment to ensure proper handling of permission results.

Sample Usage:

// In your Activity or Fragment's onCreate/onCreateView:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AFPermissions.registerLifecycle(this) // 'this' refers to the ComponentActivity
// ... rest of your onCreate logic
}

// To check if a permission is granted:
lifecycleScope.launch {
if (AFPermissions.isPermissionGranted(Permission.CAMERA)) {
// Camera permission is granted, proceed with camera operations
} else {
// Camera permission is not granted
}
}

// To request a permission:
lifecycleScope.launch {
try {
AFPermissions.requestPermission(Permission.CAMERA)
// The system dialog will be shown to the user.
// You'll typically check the permission status again after this call,
// or rely on the PermissionHost to notify you of the result.
} catch (e: Exception) {
// Handle potential errors, e.g., if getOrCreateHost was not called.
Log.e("Permissions", "Error requesting permission", e)
}
}
actual object AFPermissions

AFPermissions provides functions to check and request permissions on iOS.

This object is the iOS-specific implementation of the AFPermissions expect declaration. It uses the native iOS APIs to interact with the permission system.

Sample Usage (Swift):

Since this Kotlin code is compiled to be used from Swift in a Kotlin Multiplatform Mobile (KMM) project, the way you call it from Swift will involve the generated framework.

import UIKit
import AFCore // Import your shared module

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
checkAndRequestCamera()
checkLocation()
}

func checkAndRequestCamera() {
// AFPermissions functions are suspend functions, so they need to be called
// from a coroutine scope. In Swift, this is often handled by a helper
// or by using libraries that bridge Kotlin coroutines to Swift (e.g., KMP-NativeCoroutines).

// For simplicity, this example uses a basic completion handler approach.
// In a real app, you'd likely use a more robust solution for handling suspend functions.

AFPermissions.shared.isPermissionGranted(permission: Permission.camera) { [weak self] isGranted, error in
guard let self = self else { return }
if let error = error {
print("Error checking camera permission: \(error.localizedDescription)")
return
}

if let isGranted = isGranted?.boolValue, isGranted {
print("Camera permission is already granted.")
// Proceed with camera functionality
} else {
print("Camera permission is not granted. Requesting...")
AFPermissions.shared.requestPermission(permission: Permission.camera) { _, error in
if let error = error {
print("Error requesting camera permission: \(error.localizedDescription)")
return
}
// After this call, the system dialog will be shown to the user.

Functions

Link copied to clipboard
actual suspend fun getPermissionStatus(permission: Permission): PermissionStatus
actual suspend fun getPermissionStatus(permission: Permission): PermissionStatus
Link copied to clipboard
fun granted(perm: String): Boolean
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun openAppSettings(context: Context = AFContextProvider.get())
Link copied to clipboard
Link copied to clipboard
fun openNotificationsSettings(context: Context = AFContextProvider.get())
Link copied to clipboard

Call from Activity.onCreate() (or a base activity)

Link copied to clipboard
actual suspend fun requestPermission(permission: Permission): PermissionStatus

Backwards-compat API: request and ignore status (logs it)

actual suspend fun requestPermission(permission: Permission): PermissionStatus
Link copied to clipboard
Link copied to clipboard

Optional: call from Activity.onDestroy() if you want explicit cleanup