feat(ios): support async Swift plugin methods (completionHandler:) in PluginManager (#14148)

* Added selector with completionHandler handling

* Added .changes file

* fix change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Vladimir Pankratov 2025-10-02 05:00:44 -07:00 committed by GitHub
parent 6bbb530fd5
commit 2e089f6acb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": minor:feat
---
Support async Swift plugin methods (`completionHandler:`) in PluginManager

View File

@ -59,8 +59,23 @@ public class PluginManager {
func invoke(name: String, invoke: Invoke) {
if let plugin = plugins[name] {
ipcDispatchQueue.async {
let selectorWithCompletionHandler = Selector(("\(invoke.command):completionHandler:"))
let selectorWithThrows = Selector(("\(invoke.command):error:"))
if plugin.instance.responds(to: selectorWithThrows) {
if plugin.instance.responds(to: selectorWithCompletionHandler) {
let completion: @convention(block) (NSError?) -> Void = { error in
if let error = error {
invoke.reject("\(error)")
}
}
let blockObj: AnyObject = unsafeBitCast(completion, to: AnyObject.self)
let imp = plugin.instance.method(for: selectorWithCompletionHandler)
typealias Fn = @convention(c) (AnyObject, Selector, Invoke, AnyObject) -> Void
let fn = unsafeBitCast(imp, to: Fn.self)
fn(plugin.instance, selectorWithCompletionHandler, invoke, blockObj)
} else if plugin.instance.responds(to: selectorWithThrows) {
var error: NSError? = nil
withUnsafeMutablePointer(to: &error) {
let methodIMP: IMP! = plugin.instance.method(for: selectorWithThrows)