{"id":1579,"date":"2023-08-31T09:50:41","date_gmt":"2023-08-31T09:50:41","guid":{"rendered":"http:\/\/sophyonline.com\/blog\/?p=1579"},"modified":"2023-08-31T09:51:33","modified_gmt":"2023-08-31T09:51:33","slug":"widget-in-ios-17-xcode-beta-errors","status":"publish","type":"post","link":"http:\/\/sophyonline.com\/blog\/2023\/08\/widget-in-ios-17-xcode-beta-errors\/","title":{"rendered":"Widget in iOS 17 Xcode beta errors"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Xcode 15, we got improvements on widgets, which also change A LOT how we handle them in code, we need to take care now of:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Live action, animations, Time Lines &amp; App Intents<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you download the<strong> Widgets example from Apple<\/strong>, you&#8217;ll see that it won&#8217;t compile in Xcode 15 Beta 2 after create the Widget Target. This will create a boiler plate code, that sadly doesn&#8217;t compile with the previews.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first change we need to do is to add&nbsp; a container background:<a href=\"\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.containerBackground(for: .widget)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/view\/containerbackground(_:for:)\">https:\/\/developer.apple.com\/documentation\/swiftui\/view\/containerbackground(_:for:)<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this extension that someone put on Apple Forums:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extension View {\n    func widgetBackground(_ color: Color) -&gt; some View {\n        if #available(iOSApplicationExtension 17.0, macOSApplicationExtension 14.0, *) {\n            return  containerBackground(color, for: .widget)\n        } else {\n            return background(color)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use it like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AvatarView(entry.character)\n            .widgetBackground(.white)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second change is about how previews work now:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct EmojiRangerWidget_Previews: PreviewProvider {\n\n   static var previews: some View {\n      AvatarView(.panda)\n        .widgetBackground(.white)\n        .previewContext(WidgetPreviewContext(family: .systemSmall))\n   }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">See that we&#8217;re using the <strong>.widgetBackground<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>But now, with the Preview macro, <\/strong>there&#8217;s another way to use them, basically we need to use the Entries:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Preview(as: .systemSmall) {\n    EmojiRangerWidget()\n} timeline: {\n    SimpleEntry(date: .now,\n                character: .panda)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But, this will yell an error and won&#8217;t let you run the app:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>-[INIntent _initWithIdentifier:backingStore:schema:error:] Missing backing store for Intent: {xxx-xxx-xxx some ID} (N\/A - N\/A)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To fix this, we need to add In our Entry&nbsp; the configuration intent and our detail character detail model<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct SimpleEntry: TimelineEntry {\n    let date: Date\n    let configuration: ConfigurationAppIntent\n    let character: CharacterDetail\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result will now compile in our preview. Check the result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#Preview(as: .systemSmall) {\n    EmojiRangerWidget()\n} timeline: {\n    SimpleEntry(date: .now,\n                configuration: ConfigurationAppIntent(),\n                character: .panda)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And that&#8217;s it. I still see the error mentioned above, but at least is working<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In new betas, from the number 5, this is fixed, but I wanted to leave the solution here, because it was a challenge!!!!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Check the WWDC23 videos for more information<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Video: <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2023\/10028\/?time=233\">https:\/\/developer.apple.com\/videos\/play\/wwdc2023\/10028\/?time=233<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2023\/10028\/?time=241\"><u>This allows it to show up in all the new supported locations on the Mac and iPad.<\/u><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Xcode 15, we got improvements on widgets, which also change A LOT how we handle them in code, we need to take care now of: Live action, animations, Time Lines &amp; App Intents If you download the Widgets example from Apple, you&#8217;ll see that it won&#8217;t compile in Xcode 15 Beta 2 after create <a class=\"read-more\" href=\"http:\/\/sophyonline.com\/blog\/2023\/08\/widget-in-ios-17-xcode-beta-errors\/\">&hellip;&nbsp;<span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[1],"tags":[54,51,52,53,17],"class_list":["post-1579","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-error-xcode-beta","tag-ios-17","tag-widget-on-ios-17","tag-widgets","tag-xcode"],"_links":{"self":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/comments?post=1579"}],"version-history":[{"count":1,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1579\/revisions"}],"predecessor-version":[{"id":1580,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/posts\/1579\/revisions\/1580"}],"wp:attachment":[{"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/media?parent=1579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/categories?post=1579"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sophyonline.com\/blog\/wp-json\/wp\/v2\/tags?post=1579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}