Hi! :slightly_smiling_face: I have a question abo...
# ios
b
Hi! 🙂 I have a question about KMP and swift extension methods. I am developing KMP library with which I want to detect shake motion directly inside the library, so anyone using that library won’t need to do any setup to enable this feature. For Android part I was able to implement that with StartUp library where I get context and I just add some LifecycleObserver to the lifecycle to detect shaking event. But for iOS I am a little bit lost how to do that from Kotlin code. What I know so far that I need to override motionEnded method for UIWindow class. But with my KMP library I have only kotlin specific code in my library and I am not sure how to write such extension from kotlin library. Below I am attaching the method that needs to be overriden. Does anyone have an idea how to achieve that directly from Kotlin code, or if there is some other way to do that from KMP library?
Copy code
mport UIKit
extension UIWindow {
   open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
     super.motionEnded(motion, with: event)
     if motion == .motionShake {
       print("Shake detected")
     }
   }
}
j
You generally can't override methods in classes you don't own since that violates the contract of the class -- unless of course you do so through swizzling. You might be able to hook into that functionality by injecting a responder conforming to UIResponder into the responder chain. Otherwise, if you do it through a window you'll have to subclass UIWindow and make sure every app using your library uses your UIWindow subclass in their app.
b
Thank you @Jacob Rhoda for responding. With one of my colleague we found out that you can create extension for UIWindow even though you don’t have a subclass of UIWindow at all. For example this is done in this link and in this library. My main information that I am currently seeking is if there is possibility to create swift extension for some iOS component directly inside kotlin iOS source set folder within the library itself. Because I want to make the same behaviour as it is done in above mentioned library to enable shake mechanism directly in the library without any additional developer setup.
j
Hmm, it does seem that would work in Obj-C, which surprises me. You taught me something today. :)
😊 1
You could set up the override in a Obj-C framework, import it into your Kotlin framework, and then have some way to inject a Kotlin routine into it.
b
That looks like a really interesting way of doing this. Thank you a lot @Jacob Rhoda I will give it a go probably this week and let you know how it will work for me. 🙌
p
Looking at the integration steps for swift. KMP-ObservableViewModel it has:
Copy code
dependencies: [
    .package(url: "<https://github.com/rickclephas/KMP-ObservableViewModel.git>", from: "1.0.0-BETA-3")
]
Which is basically pulling the
.swift
files from the github repo when compiling. So doesn’t seem to be feasible yet.