I have 2 questions on including Swift code in your...
# ios
b
I have 2 questions on including Swift code in your Kotlin code with the help of cinterop. I am working on a library that should react on user shake gesture on the device. For Android it was quite easy to achieve this directly inside the library but for iOS is a different story. I want to use swift extension on UIWindow for that. 1. If I manage to setup my project to use cinterop would such approach where I just write an extension method inside of Swift file worked? Do I need to exposes it to objective c in order for that extension method to be active on UIWindow component when my library is included or it would automatically worked without exposing it? So far I saw only approaches that weren’t used for Swift extensions but only to call some method from Kotlin that would execute Swift code, 2. I manage to do some work on cinterop front. I prepared .def file, I prepared configuration in my gradle for my iOS targets and I also prepared objective-c header for my Swift code. I also see that my klib file gets generated which is a good sign. I read and viewed a lot of videos on cinterop topic with Swift and I know that I am still missing a part where I need to provide a binary for my header, and I am stuck here. How can I do that? I want to first cover a scenario that would run for me on my local machine? I tried adding myLib.a file to my KMP library and trying a different ways to included it into my def file. I also tried adding myLib.a file directly in my iOS sample project that was using my library but I still end up with linking error in the end. The code is in 🧵
ShakeDetectorIOS.def:
Copy code
package = sp.bvantur.inspektify.shakedetector
language = Objective-C
headers = ShakeDetectorIOS.h
linkerOpts = -lobjc -framework Foundation
ShakeDetectorIOS.h:
Copy code
#import <Foundation/Foundation.h>

@interface ShakeDetectorIOS : NSObject

//- (void)enableShakeDetector:(BOOL)condition;

@end
ShakeDetectorIOS.swift:
Copy code
import Foundation

@objc public class ShakeDetectorIOS: NSObject {
}
build.gradle.kts:
Copy code
listOf(
        iosArm64(),
        iosX64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "InspektifyLib"
            isStatic = true
        }
        it.compilations.getByName("main") {
            cinterops.create("ShakeDetectorIOS") {
                defFile = file("src/nativeInterop/cinterop/ShakeDetectorIOS.def")
                packageName = "sp.bvantur.inspektify.shakedetector"
                includeDirs.allHeaders("src/nativeInterop/cinterop/")
                extraOpts("-libraryPath", "$projectDir/src/nativeInterop/cinterop/")
            }
        }
    }