Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS Image Picker error: "connection to service named com.apple.FileProvider" when pick camera images (screenshots work fine) #254

Open
SajadGarshasbi opened this issue Feb 7, 2025 · 0 comments

Comments

@SajadGarshasbi
Copy link

Summary

I'm experiencing a crash on iOS when using the Calf library in my Compose KMP project. After selecting a file using the file picker, the following error is logged:

[default] [ERROR] Could not create a bookmark: NSError: Cocoa 4097 “connection to service named com.apple.FileProvider”

This error appears to be associated with camera-taken images—when the selected file is an image captured by the camera, the app crashes during image decoding (Image.makeFromEncoded). In contrast, screenshots and other images load without issue.

my full code:

Column {
    val galleryPermissionState = rememberPermissionState(Permission.Gallery)
    
    if (!galleryPermissionState.status.isGranted) {
        Button(onClick = { galleryPermissionState.launchPermissionRequest() }) {
            Text("Request Gallery Permission")
        }
    } else {
        Text("Test Image Picker")
        val scope = rememberCoroutineScope()
        val context = LocalPlatformContext.current
        var imageBitmap: ImageBitmap? by remember { mutableStateOf(null) }
        var imageByteArray: ByteArray? by remember { mutableStateOf(null) }
        var uploadLink: String by remember { mutableStateOf("") }
        
        val pickerLauncher = rememberFilePickerLauncher(
            type = FilePickerFileType.Image,
            selectionMode = FilePickerSelectionMode.Single,
            onResult = { files ->
                scope.launch {
                    files.firstOrNull()?.let { file ->
                        val data = file.readByteArray(context)
                        imageByteArray = data
                        imageBitmap = data.toImageBitmap()  // Crashes here for camera images
                    }
                }
            }
        )
        
        PrimaryButton(
            modifier = Modifier.padding(8.dp),
            title = "Pick Image",
            containerColor = getColors().primary.c900,
            isLoading = false,
            onClick = { pickerLauncher.launch() }
        )
        
        imageBitmap?.let {
            Image(
                bitmap = it,
                contentDescription = "Selected Image",
                modifier = Modifier.size(200.dp),
                contentScale = ContentScale.Crop
            )
            PrimaryButton(
                modifier = Modifier.padding(8.dp),
                title = "Upload",
                containerColor = getColors().primary.c900,
                isLoading = false,
                onClick = {
                    scope.launch {
                        AppApi.instance.uploadFile("test1.jpeg", imageByteArray!!, "service-bucket")
                            .run {
                                data?.let {
                                    uploadLink = it.url
                                    println("Uploaded image url is: ${it.url}")
                                }
                                err?.let {
                                    uploadLink = it.getErrorMessage()
                                    println("upload error: ${it}")
                                }
                            }
                    }
                }
            )
            
            if (uploadLink.isNotEmpty()) {
                Text(uploadLink)
            }
        }
    }
}

also i add this line to my info.plist

<key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to your photo library to select images.</string>
    <key>NSCameraUsageDescription</key>
    <string>Camera permission is required to take pictures</string>

the code on Android and Desktop works fine, i tested it on real device iPhone 15 pro max with iOS 18.3, calf library version 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant