Hello! I am stuck with the following problem: I am...
# kotlin-native
f
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
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
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