https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
f

Federico Torres

10/18/2023, 4:56 PM
Hello! I am stuck with the following problem: I am implementing NFC logic in kmp, I need to implement the protocol
NFCNDEFReaderSessionDelegateProtocol
from kotlin code. The problem is that this protocol was translated from OBJ-C to Kotlin like this:
Copy code
public expect interface NFCNDEFReaderSessionDelegateProtocol : platform.darwin.NSObjectProtocol {
    @kotlin.commonizer.ObjCCallable public open expect fun readerSession(session: platform.CoreNFC.NFCNDEFReaderSession, didDetectTags: kotlin.collections.List<*>): kotlin.Unit { /* compiled code */ }

    @kotlin.commonizer.ObjCCallable public abstract expect fun readerSession(session: platform.CoreNFC.NFCNDEFReaderSession, didDetectNDEFs: kotlin.collections.List<*>): kotlin.Unit

    @kotlin.commonizer.ObjCCallable public abstract expect fun readerSession(session: platform.CoreNFC.NFCNDEFReaderSession, didInvalidateWithError: platform.Foundation.NSError): kotlin.Unit

    @kotlin.commonizer.ObjCCallable public open expect fun readerSessionDidBecomeActive(session: platform.CoreNFC.NFCNDEFReaderSession): kotlin.Unit { /* compiled code */ }
}
Meaning that there are two
readerSession
methods with the exact same signature, thus I am unable to implement all the abstract methods of this interface from Kotlin code
a

Anton Saatze

10/19/2023, 9:37 AM
I was facing the same issue with the
CBCentralManagerDelegate
https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate?language=objc The workaround I came up with: provide an Interface, that has all your callback Methods and some swiftcode has to implement the interface and calls them. In your case:
Copy code
NFCNDEFReaderSessionDelegateProtocolInterface {
// the two you need
fun readerSessionDidDetectTags(session: platform.CoreNFC.NFCNDEFReaderSession, didDetectTags: kotlin.collections.List<*>)
fun readerSessionDidDetectNDEFs(session: platform.CoreNFC.NFCNDEFReaderSession, didDetectTags: kotlin.collections.List<*>)
// for consistency
fun readerSessionDidInvalidateWithError(session: platform.CoreNFC.NFCNDEFReaderSession, didInvalidateWithError: platform.Foundation.NSError)
fun readerSessionDidBecomeActive(session: platform.CoreNFC.NFCNDEFReaderSession)
}
Let your Switftcode inherit these methods and call them when the swift
func
gets called. And eventually inject that class which implemented NFCNDEFReaderSessionDelegateProtocolInterface into your shared code.
f

Federico Torres

10/19/2023, 2:59 PM
yeah your solution seems to be the only possible solution here, I wanted to avoid adding code from client side but seems like I don't have any other choice. Thank you Anton
a

Anton Saatze

11/06/2023, 6:23 AM
3 Views