ziv kesten
10/31/2024, 3:10 PMOSTUserAttributes
in the iosMain folder, however that class seems to be unavailable.
I have created a wrapper for that class.
import Foundation
import OSTSDK // Import the SDK containing OSTUserAttributes
@objc public class KmmUserAttributes: NSObject {
private var builder: OSTUserAttributes.Builder
private var userAttributes: OSTUserAttributes?
public override init() {
self.builder = OSTUserAttributes.Builder()
super.init()
}
@objc public func setFirstName(_ name: String) {
builder = builder.withFirstName(name)
}
}
And created a bridging header iosApp/iosApp-Bridging-Header.h
#import "iosApp-Swift.h"
#import "KmmUserAttributes.h"
I cleaned, i built, but whatever i do i have not managed to access KmmUserAttributes
in iosMain
Is there any other approach for accessing swift files from iosApp in iosMain?Matthew Feinberg
11/01/2024, 3:23 AMinterface SwiftKotlinBridge {
fun constructKmmUserAttributes() : KmmUserAttributes
// Whatever other Swift APIs you want to call from Kotlin would go here
companion object {
val INSTANCE get() = swiftKotlinBridgeInstance ?: throw IllegalStateException()
}
}
private var swiftKotlinBridgeInstance : SwiftKotlinBridge? = null
fun registerSwiftKotlinBridge( instance: SwiftKotlinBridge ) {
swiftKotlinBridgeInstance = instance
}
interface KmmUserAttributes {
fun setFirstName( name: String )
}
// It's a function that looks like a constructor
// you could even move the interface to common code
// and make the "constructor" function expect/actual
fun KmmUserAttributes() = SwiftKotlinBridge.INSTANCE.constructKmmUserAttributes()
And then in my Swift code, I just make a class that implements the SwiftKotlinBridge
and on launch (before any KMM or CMP code is executed) I just call registerSwiftKotlinBridge()
from the Swift side.yousefa2
11/01/2024, 8:36 AMKmmUserAttributes
class in its own pod and declare it as a dependency for your KMP module. This will allow the compiler to link it and will generate the kotlin code to access it.ziv kesten
11/03/2024, 6:36 AMyousefa2
11/03/2024, 9:05 PM