Hey guys, Im currently making a cmp app for androi...
# multiplatform
b
Hey guys, Im currently making a cmp app for android and ios. I need to display a pdf in the app and haven't found a library for it. I implemented it myself in android, but im having some problems with ios. It's my first app and I'm getting really confused. I use this git repo as a reference (https://github.com/swapnil-musale/KMP-PDF-Viewer/blob/master/shared/src/iosMain/kotlin/com/devx/pdfviewerkmp/di/ViewModelModules.kt), I could understand the android part but im really confused for the ios part. Could somebody with a better understanding explain it quickly to me? How do I call swift code from my kotlin code? Also, what is the use of iosApp and iosMain, where am I supposed to write my platform specific code?
s
This repository is created to demonstrate how we can display PDF in KMP project using (Jetpack Compose and Swift UI). To write platform specific code use
iosMain
sourceSet
To use
PDFView
component in ComposeMultiplatform you can use
UIKitView
for more info check here : https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-uikit-integration.html#use-uikit-inside-compose-multiplatform
b
Do you know how give the pdf to
PDFView
? I have tried it earlier, but I dont what to give it so it displays my pdf. The pdf is hosted online for the moment. The repo was really usefull for android but I dont have the knowledge at the moment for it to help me in ios
s
To display PDF, I have passed base64 string to PDFView
b
So I have this right now:
Copy code
val encoded = Base64.encode(pdf.value)
UIKitView(
    factory = { PDFView() },
    modifier = Modifier.fillMaxSize(),
    update = {
        it.autoScales = true
        it.document = PDFDocument(/* what to give here */)
    }
)
I have tried to create the
PDFDocument
, but I haven't found a way for it to have the right type so it compiles. It wants either an
NSURL
or
NSData
, but I was unable to create it. l wasn't able to translate your code from swift to kotlin using a
UIKitView
. Do you know how I could do it? Our project structure are really different so im having a hard time doing it from your project
I was able to make it work following another question that was asked here a couple of months ago. This is what I have right now that works:
Copy code
UIKitView(modifier = Modifier.fillMaxSize(),
    factory = {
        WKWebView()
    },
    update = { webView ->
        webView.loadRequest(
            NSURLRequest.requestWithURL(
                NSURL.URLWithString("<https://myreport.altervista.org/Lorem_Ipsum.pdf>")!!
            )
        )
    }
)
🎉 1
s
I have not tried loading pdf from UIKitView, will try in my spare time and update you
b
thank you very much for your help
🙌 1