Can I pass a function `(UIBackgroundFetchResult) -...
# multiplatform
a
Can I pass a function
(UIBackgroundFetchResult) -> void
from swift to Kotlin? Currently the method I have written in kotlin expects a
(UIKUIBackgroundFetchResult) -> void
when called from swift.
s
I did a quick test, and yes you can do that. I had a function like:
Copy code
fun doSomething(with: String, completion: (Time) -> Void) { }
And it was callable from Swift code without problem
a
Thanks. I still have the problem of swift expecting a
UIKUIBackgroundFetchResult
instead of UIBackGroundFetchResult though.
s
Oh I see… sorry I didn’t understand the question 😅 My bad. So on the Swift side it’s called
UIBackgroundFetchResult
but KMM translate it to
UIKUIBackgroundFetchResult
🤔 Hum… considering they are basically the same but with different names, I would cast the UIKUI version to UI. But this would need to be done on the Swift side.
a
Just tried this. Unfortunately it doesn't work.
a
Got the same issue. Did you get that sorted @Aditya Kurkure? I ended up returning result back to swift and handling it there, but that is not a perfect solution.
Copy code
IOSFileKt.awaitResult { result in
            if result.isFailure {
                completionHandler(.failed)
            } else {
                completionHandler(.newData)
            }
        }
a
Yeah I did something similar. Haven't been able to find any other workaround.
d
I was running into this same issue and stumbled upon this thread via search. Here's what I have in
iosMain
:
Copy code
fun kotlinMethodReturningUIBackgroundFetchResult(): UIBackgroundFetchResult {
  // Do some work, then return something like:
  return UIBackgroundFetchResult.UIBackgroundFetchResultNewData
}
Here's how I handle this in Swift:
Copy code
// Even though the kotlin method returns `UIBackgroundFetchResult`
// it gets translated to `UIKUIBackgroundFetchResult`
// when exported to ObjC
let uikUIBackgroundFetchResult = kotlinMethodReturningUIBackgroundFetchResult()

// Convert the `UIKUIBackgroundFetchResult` back to the expected `UIBackgroundFetchResult` type
let rawValue = UInt(uikUIBackgroundFetchResult.value)
let backgroundFetchResult = UIBackgroundFetchResult(rawValue: rawValue)
Not ideal, but keeps the conversation back to
UIBackgroundFetchResult
on the Swift side relatively straightforward.