https://kotlinlang.org logo
#ios
Title
# ios
t

Trey

11/09/2023, 4:07 PM
Can somebody point me to a sample KMP app that uses SwiftUI with "@main" and UIApplicationDelegateAdaptor to use an AppDelegate defined in the iosMain section of the shared module? I can't get it to work.
It seems to me that this may be a bug with how kotlin native is implemented. There are two methods in UIApplicationDelegateProtocol that have the same name and same parameters. In swift the parameters have different names, but I don't know of a way to do this in kotlin when defining the overrides. Maybe this is just a Lint error.
Copy code
@kotlin.commonizer.ObjCCallable public open expect fun application(application: platform.UIKit.UIApplication, didFinishLaunchingWithOptions: kotlin.collections.Map<kotlin.Any?, *>?): kotlin.Boolean { /* compiled code */ }

    @kotlin.commonizer.ObjCCallable public open expect fun application(application: platform.UIKit.UIApplication, willFinishLaunchingWithOptions: kotlin.collections.Map<kotlin.Any?, *>?): kotlin.Boolean { /* compiled code */ }
d

Darron Schall

11/10/2023, 8:57 PM
Seems like a bug? I can reproduce this by making an AppDelegate in Kotlin with both methods defined. I get the following error:
Conflicting overloads: public open fun application(application: UIApplication, willFinishLaunchingWithOptions: Map<Any?, *>?): Boolean defined in AppDelegate, public open fun application(application: UIApplication, didFinishLaunchingWithOptions: Map<Any?, *>?): Boolean defined in AppDelegate
Can't try to differentiate with
@ObjCName
either, due to:
@ObjCName is not applicable on overrides
As a workaround I think you'll want to create your AppDelegate in Swift and call into your shared library code from the Swift methods.
👍 1
r

ribesg

11/15/2023, 2:23 PM
You can just suppress conflicting overloads
t

Trey

11/15/2023, 2:24 PM
How would I do that?
d

Darron Schall

11/15/2023, 2:25 PM
Looks like you also have to suppress param name changing. This is the secret sauce:
Copy code
@Suppress("CONFLICTING_OVERLOADS", "PARAMETER_NAME_CHANGED_ON_OVERRIDE")
r

ribesg

11/15/2023, 2:25 PM
If you don't actually use both functions then you just need
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
I use this one in classes implementing
UITableViewDelegateProtocol
, I think it covers all the "this is possible in Swift/ObjC but not in Kotlin" issues you can sometimes have
Copy code
@file:Suppress(
    "CONFLICTING_OVERLOADS",
    "PARAMETER_NAME_CHANGED_ON_OVERRIDE",
    "RETURN_TYPE_MISMATCH_ON_OVERRIDE"
)
👌 1
t

Trey

11/15/2023, 2:41 PM
Thank you
3 Views