{"id":35965,"date":"2022-05-23T15:26:30","date_gmt":"2022-05-23T19:26:30","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35965"},"modified":"2022-07-12T15:00:32","modified_gmt":"2022-07-12T19:00:32","slug":"how-to-open-a-window-in-swiftui","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/05\/23\/how-to-open-a-window-in-swiftui\/","title":{"rendered":"How to Open a Window in SwiftUI"},"content":{"rendered":"<p><a href=\"https:\/\/stackoverflow.com\/questions\/64823492\/how-to-open-another-window-in-swiftui-macos\">Rob N<\/a>:<\/p>\n<blockquote cite=\"https:\/\/stackoverflow.com\/questions\/64823492\/how-to-open-another-window-in-swiftui-macos\"><p>I&rsquo;d like to show a second window with different content in a SwiftUI app on macOS. I can&rsquo;t find any documentation on this.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/blog.malcolmhall.com\/2020\/12\/06\/open-window-scene-in-swiftui-2-0-on-macos\/\">Malcolm Hall<\/a> (<a href=\"https:\/\/twitter.com\/malhal\/status\/1434833704858406915\">tweet<\/a>, <a href=\"https:\/\/developer.apple.com\/forums\/thread\/651592?answerId=651132022#651132022\">forum<\/a>):<\/p>\n<blockquote cite=\"https:\/\/blog.malcolmhall.com\/2020\/12\/06\/open-window-scene-in-swiftui-2-0-on-macos\/\"><p>In your <code>ContentView<\/code> create a button and open a URL for your app and another View e.g. <code>Viewer<\/code> to be shown in the window we will open[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>In your <code>App<\/code> add another <code>WindowGroup<\/code> for your viewer and set it to enable handling of external launch events (an internal event in our case).<\/p>\n<p>[&#8230;]<\/p>\n<p>Now in Project-&gt;Info-&gt;URL Types type in myappname in the URL Schemes field (and the identifier field too) to register our app with the system.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/tonyarnold\/status\/1434845723770515458\">Tony Arnold<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/tonyarnold\/status\/1434845723770515458\">\n<p>Is that a workaround? That&rsquo;s a workaround for missing API, right? &#x1F633;<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/malhal\/status\/1434848464521863173\">Malcolm Hall<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/malhal\/status\/1434848464521863173\">\n<p>No, its a declarative state-driven API that handles the reason why you would want a new window. You aren&rsquo;t going to get an imperative open window in SwiftUI because that&rsquo;s not how it is designed.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/krzyzanowskim\/status\/1434890472514887693\">Marcin Krzyzanowski<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/krzyzanowskim\/status\/1434890472514887693\">\n<p>I&rsquo;m not convinced this is designed. It&rsquo;s more like something that happens, but doesn&rsquo;t really is well thought for the scenario where the app needs to open multiple windows. URL cannot be the way. It&rsquo;s missing window state management. Windows are hierarchical. This is not.<\/p>\n<\/blockquote>\n\n<p>It&rsquo;s great that there <em>is<\/em> a way to do this without <a href=\"https:\/\/developer.apple.com\/forums\/thread\/651592\">calling into AppKit<\/a>, but using URLs seems gross.<\/p>\n\n<p><a href=\"https:\/\/onmyway133.com\/posts\/how-to-manage-windowgroup-in-swiftui-for-macos\/\">Khoa Pham<\/a>:<\/p>\n<blockquote cite=\"https:\/\/onmyway133.com\/posts\/how-to-manage-windowgroup-in-swiftui-for-macos\/\"><p>A common trick is to use an <code>NSViewRepresentable<\/code> to get underlying <code>view.window<\/code> and attach this to our SwiftUI view<\/p>\n<p>[&#8230;]<\/p>\n<p>For a common macOS SwiftUI app with a search bar on the toolbar and a normal settings screen, here&rsquo;s how I usually do<\/p>\n<p>Setup NSWindow programmatically<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/02\/10\/micro-blog-moving-ios-app-to-react-native\/\">Micro.blog Moving iOS App to React Native<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/08\/11\/1password-8-for-mac-early-access\/\">1Password 8 for Mac Early Access<\/a><\/li>\n<\/ul>\n\n<p id=\"how-to-open-a-window-in-swiftui-update-2022-07-12\">Update (2022-07-12): <a href=\"https:\/\/nilcoalescing.com\/blog\/ProgrammaticallyOpenANewWindowInSwiftUIOnMacOS\/\">Natalia Panferova<\/a> (<a href=\"https:\/\/twitter.com\/natpanferova\/status\/1545329961262669824\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/ProgrammaticallyOpenANewWindowInSwiftUIOnMacOS\/\"><p>In macOS 13 we finally have a way to programmatically present a window in SwiftUI. We can call the new <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/environmentvalues\/openwindow\">openWindow<\/a> action from the environment and pass it a scene id or a value.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/06\/17\/swiftui-changes-at-wwdc-2022\/\">SwiftUI Changes at WWDC 2022<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Rob N: I&rsquo;d like to show a second window with different content in a SwiftUI app on macOS. I can&rsquo;t find any documentation on this. Malcolm Hall (tweet, forum): In your ContentView create a button and open a URL for your app and another View e.g. Viewer to be shown in the window we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-05-23T19:26:32Z","apple_news_api_id":"9518eb20-24e3-494f-8b56-e806ba58f0cb","apple_news_api_modified_at":"2022-07-12T19:00:35Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AlRjrICTjSU-LVugGuljwyw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[30,2077,71,901,1812],"class_list":["post-35965","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-mac","tag-macos-12","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=35965"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35965\/revisions"}],"predecessor-version":[{"id":36483,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35965\/revisions\/36483"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}