How can I call an async function in iOS custom plugin? #7282
Unanswered
DaDlugosch
asked this question in
Q&A
Replies: 1 comment
-
Good point. This is also a limitation on the Android side. Unfortunately you can't have the One way to solve this would be to switch into an async context within the synchronous function. So in iOS you could invoke a Task where you process the work and resolve the plugin call. @objc
func mySynchronousPluginMethod(_ call: CAPPluginCall) {
Task {
let success = await myAsyncMethod()
if (success) {
call.resolve()
} else {
call.reject("my error")
}
}
} Android equivalent might look like this. @PluginMethod
fun mySynchronousPluginMethod(call: PluginCall) {
GlobalScope.launch {
val success = mySuspendingMethod()
if (success) {
call.resolve()
} else {
call.reject("my error")
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I've created a custom iOS plugin which I inject and call inside my JS function. I would like to use it as an oauth authentication middleware, however I need to call
try await
inside it and based on that the function must beasync
. But when I set an async keyword to my function:@objc func echo(_ call: CAPPluginCall) async {}
after calling it inside my app I've got this error response:When I call this method without
async
keyword, everything works (excepttry await
function call, of course)Beta Was this translation helpful? Give feedback.
All reactions