is there any good resources on the best ways to ex...
# multiplatform
j
is there any good resources on the best ways to execute swift code from kmp? is it literally make an expected and actual, and make the code call back to the ios project via a manually made bridge? is there an easier way using interop? Asking because im trying to implement sdks like stripe, zendesk and segment
j
Yeah, I think the general approach used is to have say Swift class implement some shared Kotlin interface and pass instance of that Swift class to shared code (e.g. when intialising DI)
j
yeah ok thats what i thought/expected. good enough thanks! just wanetd to make sure there wasnt a better way before diving into that implementation style, thanks!
j
the other advantage often of that approach is that the swift code can make use of swift packages that SDKs like that typically provide
1
☝🏿 1
a
We actually covered that question with a recent addition to our documentation 🙂 https://kotlinlang.org/docs/native-lib-import-stability.html#swift-library-import Also, there's always an option of "making an objective-c wrapper" (annotate your public swift api with
@objc
if good enough) and consume that through cinterop 🙂 But that way is more challenging. There are couple of helpers to automate the process, spm4kmp I think is the most stable at the moment.
👀 1
thank you color 1
❤️ 1
f
@John O'Reilly using this approach with suspend function in the interface is not really stable, I got some issues
j
what sort of issues? I've used this approach with several SDKs without issue. I've also typically been using SKIE as well fwiw
(with suspend functions)
f
Mainly frozen execution from the swift implementation, I was implementing/bridging Airship, I didn’t dig a lot about the reason.
j
well, that doesnt lead to any useful insights. ^ 😛 ok well ill try it and if I have issues ill dig into it and report back here there 🙂
f
Anyway I’m the maintainer of spmforkmp, open a discussion on the project if you need help for using the plugin
🙌 2
thank you color 1
p
The react-native community basically does this using turbo modules. Is basically the same principle, you declare an interface/class in Typescript and a gradle plugin will generate all the jni/obj-c++ bindings for the bridge that you consume in kotlin/swift. It is pretty standard in that community with a lot of support from the rn team and major framework providers. I wonder if KMP could have something similar, or promote more plugins like Francois above, which is similar to what the RN turbo modules do.
f
Well Kotlin cinterop does basically this: you create an ObjC header, and then Kotlin can imports and transforms the header into Kotlin code.
spmForKmp make this process more convenient
p
Right, the functionality is intrinsic in KMP already, it is more a matter of, everybody is doing the bridge their way vs having a common bridge API from KMP itself that all projects standardize. Actually using Koin makes the bridging very simple.