Home > Net >  How to use interface in swift with Kotlin multiplatform moblie
How to use interface in swift with Kotlin multiplatform moblie

Time:05-06

I am trying to use interface in swift, but it unable to find the property

commainMain

interface ApplicationToken {
    val accessToken: String
    val refreshToken: String
}

iosMain

Platform.kt

lateinit var tokenProvider: ApplicationToken

HttpClient.kt

actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(Darwin) {
    config(this)
    engine {
        configureRequest {
            setAllowsCellularAccess(true)
        }
    }
    install(Auth) {
        bearer {
            loadTokens {
                BearerTokens(tokenProvider.accessToken, "")
            }
        }
    }
}

Now when I am to access tokenProvider in my swift code. It cannot find. I am adding image please have a look.

enter image description here

I tried another options to create class and implement my interface and call the class

class getToken : ApplicationToken {
  let accessToken: String = ""
  let refreshToken: String = ""
}

_ = getToken()

but it giving me error. I cannot paste it because I don't understand in xcode. Thanks

CodePudding user response:

When generating code for extensions (e.g. fun SomeClass.extension()) and for global variables, like in your case, Kotlin creates kind of a namespace, related to the filename.

In your case your property should be under PlatformKt.tokenProvider.


Usually when it's hard to find how your is visible on iOS side, it's useful to check out framework header file. It should be places somewhere around here(replace shared with your framework name):

shared/build/cocoapods/framework/shared.framework/Headers/shared.h

I prefer adding this file by reference in my Xcode, so I can have fast access all the time:

enter image description here

Then you can easily search through this file for your variable/class/etc name.

  • Related