iOS: Present a new view controller from a Tauri plugin #14909
Answered
by
setoelkahfi
setoelkahfi
asked this question in
Q&A
-
|
I'm learning to develop an iOS plugin that will simply open a new view controller from an invoke method. Since I got the view controller in the class IosWindow: Plugin {
@objc public func ping(_ invoke: Invoke) throws {
let args = try invoke.parseArgs(PingArgs.self)
guard let viewController = manager.viewController else {
print("Cannot get root view controller.")
return
}
//Task { @MainActor in
//viewController.modalPresentationStyle = .popover
//viewController.present(UIViewController(), animated: true)
// Same effect:
// viewController.navigationController?.pushViewController(UIViewController(), animated: true)
//}
// Commented this, no luck
invoke.resolve(["value": "Olaaaa, \(args.value ?? "default"), \(viewController.description)"])
}
I tried using |
Beta Was this translation helpful? Give feedback.
Answered by
setoelkahfi
Feb 7, 2026
Replies: 1 comment
-
|
Never mind, this looks good: @objc public func ping(_ invoke: Invoke) throws {
let args = try invoke.parseArgs(PingArgs.self)
guard let viewController = manager.viewController else {
print("Cannot get root view controller.")
invoke.reject("Cannot get root view controller.")
return
}
// Resolve immediately before presenting
invoke.resolve(["value": "Popover presenting with value: \(args.value ?? "default")"])
// Present on main thread
DispatchQueue.main.async {
// Create a new view controller for the popover
let popoverVC = UIViewController()
popoverVC.view.backgroundColor = .systemBackground
// Add a label to show the ping value
let label = UILabel()
label.text = "Ping: \(args.value ?? "default")"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
popoverVC.view.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: popoverVC.view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: popoverVC.view.centerYAnchor),
label.leadingAnchor.constraint(equalTo: popoverVC.view.leadingAnchor, constant: 20),
label.trailingAnchor.constraint(
equalTo: popoverVC.view.trailingAnchor, constant: -20),
])
// Configure popover presentation
popoverVC.modalPresentationStyle = .popover
popoverVC.preferredContentSize = CGSize(width: 300, height: 200)
if let popover = popoverVC.popoverPresentationController {
popover.sourceView = viewController.view
popover.sourceRect = CGRect(
x: viewController.view.bounds.midX,
y: viewController.view.bounds.midY,
width: 0,
height: 0)
popover.permittedArrowDirections = [.up, .down]
}
// Present the popover
viewController.present(popoverVC, animated: true)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
setoelkahfi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Never mind, this looks good: