在 2020 年 Apple 平台 API 测试版中,添加了主题中提到的方法。(类似的方法已添加到AnyView。)有谁知道首先发送外部事件的相应API在哪里?
使用发送在 macOS 中打开新窗口的示例Scene.handlesExternalEvents
@main
struct TestAppApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
//subscribe on event of open mainView
.handlesExternalEvents(matching: Set(arrayLiteral: Wnd.mainView.rawValue))
WindowGroup {
HelperView()
}
//subscribe on event of open helperView
.handlesExternalEvents(matching: Set(arrayLiteral: Wnd.helperView.rawValue))
}
}
enum Wnd: String, CaseIterable {
case mainView = "MainView"
case helperView = "OtherView"
func open(){
if let url = URL(string: "taotao://\(self.rawValue)") {
print("opening \(self.rawValue)")
NSWorkspace.shared.open(url)
}
}
}
Run Code Online (Sandbox Code Playgroud)
和 2 个窗口/视图代码:
extension TestAppApp {
struct MainView: View {
var body: some View {
VStack {
Button("Open Main View") {
Wnd.mainView.open()
}
Button("Open Other View") {
Wnd.helperView.open()
}
}
.padding(150)
}
}
struct HelperView: View {
var body: some View {
HStack {
Text("This is ") + Text("Helper View!").bold()
}
.padding(150)
}
}
}
Run Code Online (Sandbox Code Playgroud)
Info.plist 还需要更改:
| 归档时间: |
|
| 查看次数: |
1589 次 |
| 最近记录: |