Hello everyone, i have a problem:) I want to conne...
# multiplatform
d
Hello everyone, i have a problem:) I want to connect a self made iOS framework to my kmm project, and using cocoapods for that reason, but i can't get my head over one thing, whatever I do, I cant see and call my swift code from kmm side. At the same time my objective c code is perfectly available from kmm side, no matter how poorly my framework is made, the problem is - i dont know the objective c. The reason why i need a framework is that I have a protobuf + grpc backend, and there is no, known to me, implementation of that for kmm, that can offer both android and iOS. So i just decided to make two platform libs with protoc and the magic of wrapping, with an android side it went quite well, but I'm facing issues with an iOS, where I'm obviously not an expert Here is an example of my swift class
Copy code
import Foundation

@objc public class SampleDataObject: NSObject {
    var id: String = ""
    var count = 0
    
    public init(id: String, count: Int = 0) {
        self.id = id
        self.count = count
    }
    
    public func toString() -> String {
        return id + " " + String(count)
    }
    
    public func equals(other: SampleDataObject) -> Bool {
        return other.id == id
    }
}
And here is my objC class
Copy code
#import <Foundation/Foundation.h>
#import <VAGrpcOim/AudioProxy.pbobjc.h>
#import <VAGrpcOim/AudioProxy.pbrpc.h>

NS_ASSUME_NONNULL_BEGIN

@interface TestClass : NSObject

@property (strong) NSString *firstName;
@property (strong) NSString *lastName;

- (int) sumWithFirst: (int) a Second: (int) b;
- (void) test;

@end

NS_ASSUME_NONNULL_END
Copy code
#import "TestClass.h"

@implementation TestClass

- (int) sumWithFirst: (int) a Second: (int) b{
    return a+b;
}

- (void) test {
    //
}

@end
What I do wrong? Is somewhere out there I can find an article that explains how to make a swift based framework, that could be connected to KMM project?
đź‘€ 1
l
None of your methods are marked objc. I believe both have to be marked. You should be able to see a class SampleDataObject, but it won’t have any methods unless you mark them objc.
k
You’re not really meant to call the swift side from KMM, as its really a framework. Unless you’re passing it a callback or an interface. What use case is this for? Unless do you mean your cocoapod is being added in the kmm library?
l
There are times where certain iOS libraries are not supported by obj-c, and times where the runtime has odd effects (some CF libraries take in a hashmap with params, and Kotlin likes to set ObjHeaders, which the CF library can’t understand). In these cases, you’ll need Obj-C or Swift code to do the heavy lifting.
Here's an example where I needed Swift to set the param hashmap for a CF call. There’s not an equivalent issue in Android, so an interface wouldn’t make much sense, when I could directly call into Swift.
The code I just posted is also known to work, so it could help the OP. I’m using a gradle plugin in the build.gralde.kts for the module that compiles swift and creates the cinterop for you. It could be helpful here.
If all you need is gRPC, you can take a look at this library
d
@Kevin S my goal is to add a platform library for an android and ios sides of my kmm based library respectively, as non transitive dependencies. The reason is that the required functionality is not yet available as kmm library. The Square Wire project does not have grpc ios functionality implemented, the Ktor is in the similar state. So, I decided to go quick and dirty: make wrapper libraries for protoc outputs. The outcome - it went just dirty on iOS side.
e
@dron247 if you haven’t found the answer yet. I have a suggestion. You could build your swift part as static library with Objective-C headers and add it in KMM library you developed via cinterop. Keep in mind that KMM doesn’t have direct Swift - Kotlin interoperability, your Swift-code should be compatible with Objective-C. There is a great article how to do this: https://lazarevzubov.medium.com/compatible-with-objective-c-swift-code-e7c3239d949 Also I’ve recently built gradle plugin, which can help you to easily build your Swift code as static library: https://github.com/ttypic/swift-klib-plugin. It supports only iOS targets now, but I will add watchOS, tvOS, macOS targets soon and I am going to actively maintain the project.
d
In the end the problem was solved by inviting an old school ios pro guy, who explained to our ios guy how to wrap swift code with objective-c in the right manner