how can I import a Swift class from `iosApp` into ...
# multiplatform
h
how can I import a Swift class from
iosApp
into common module's
iosMain
?
a
You cannot directly, but you depending on the functionality, you can refactor it into Kotlin using certain platform specific libraries made available to the iosMain source. It is a bit dense at first, but we use it for creating a UIImage from Kotlin code for example.
p
I use a getter/setter functions in iosMain, the only problem is the plugin appends the library name in front of the class name. I believe the plugin exports most of the classes for you. I just use setter functions to inject implementation of certain interfaces
a
Ahhhh, I see. We do a trick that is slightly the opposite for platform specific services. We create a simple Kotlin shared object, with properties for each platform service using an abstract class defined for that service in Kotlin shared code. Then, in Swift, we extend the Kotlin shared class for each implementation to minimize the platform specific code to the few functions needed, with the abstract class using those to do as much in Kotlin as possible. When our app initializes we simply initialize the PlatformService Kotlin properties with the platform specific implementations.
Let me know if you'd like an example and I'll see what I can share
h
the example would be great thank you
p
Not sure if it covers your needs, but check how I use that class IosBridge in bellow code. https://github.com/pablichjenkov/templato/blob/master/components/iosApp/iosApp/iosApp.swift#L7
The exported classes have a primitive convention between objective-c and kotlin, you can check what are the corresponding type mapping in each side by trying all the primitives and see what the resulting types are, in each side
h
@Pablichjenkov could you please take a look at my last question in the channel? it's the last message in channel
a
https://gist.github.com/codesplode/c0f89ca760786361020aa23250d551dc Here is a simple Gist showing a simple PlatformServices singleton object and two abstract PlatformService class for location and credentials. You can extend these abstract classes in Swift and Android code to implement only the required methods, while having a nice, complete set of methods in the abstract implementations that work for both. You only really care about getting the meaningful data out of the specific platform to use in Kotlin going forward with this model. In each platform's lifecycle you can assign or initialize the PlatformServices object properties with koin/injection, manually setting them, an initialization function, builder, however you choose.
p
I only see one file in kotlin, can you provide the swift side?
a
Sorry, I'm working quickly between my daily work and tried to keep it simple so you could infer the rest. I've updated it with a swift implementation of CredentialService and an App swift file. https://gist.github.com/codesplode/c0f89ca760786361020aa23250d551dc
p
Great man, looks really good. Thanks
❤️ 1