Skip to content

Commit 2e089f6

Browse files
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 <[email protected]>
1 parent 6bbb530 commit 2e089f6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

.changes/ios-async-support.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor:feat
3+
---
4+
5+
Support async Swift plugin methods (`completionHandler:`) in PluginManager

crates/tauri/mobile/ios-api/Sources/Tauri/Tauri.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,23 @@ public class PluginManager {
5959
func invoke(name: String, invoke: Invoke) {
6060
if let plugin = plugins[name] {
6161
ipcDispatchQueue.async {
62+
let selectorWithCompletionHandler = Selector(("\(invoke.command):completionHandler:"))
6263
let selectorWithThrows = Selector(("\(invoke.command):error:"))
63-
if plugin.instance.responds(to: selectorWithThrows) {
64+
65+
if plugin.instance.responds(to: selectorWithCompletionHandler) {
66+
let completion: @convention(block) (NSError?) -> Void = { error in
67+
if let error = error {
68+
invoke.reject("\(error)")
69+
}
70+
}
71+
72+
let blockObj: AnyObject = unsafeBitCast(completion, to: AnyObject.self)
73+
let imp = plugin.instance.method(for: selectorWithCompletionHandler)
74+
75+
typealias Fn = @convention(c) (AnyObject, Selector, Invoke, AnyObject) -> Void
76+
let fn = unsafeBitCast(imp, to: Fn.self)
77+
fn(plugin.instance, selectorWithCompletionHandler, invoke, blockObj)
78+
} else if plugin.instance.responds(to: selectorWithThrows) {
6479
var error: NSError? = nil
6580
withUnsafeMutablePointer(to: &error) {
6681
let methodIMP: IMP! = plugin.instance.method(for: selectorWithThrows)

0 commit comments

Comments
 (0)