Getting Started With Widgets | raywenderlich.com

In this tutorial, youā€™ll add a widget to a large SwiftUI app, reusing its views to show entries from the appā€™s repository.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/11303363-getting-started-with-widgets

It is only for members right? Because I downloaded and obviously when I login in the simulator it says, I need to be a member :slight_smile: It was not clear from the tutorial, I thought I just did not need to be a professional subscriber. It makes sense I guess otherwise I would have access to all your videos. I might subscribe one day :+1:
I did not know your app is open source. All in SwiftUI and Combine. Impressive!

1 Like

ah, of course, the app streams the video courses! sorry! you donā€™t need to have a professional subscription, but I guess you do need the standard subscription. Iā€™ll ask the gatekeeper if we can do anything.

1 Like

hi Laurent! Thanks for your patience ā€” Iā€™ve replaced the projects with a version that allows non-subscribers to fetch the list of tutorials. And Iā€™ve added a note at the top about non-subscribers. Thanks for the heads up!

2 Likes

Thanks for making this tutorial! I really liked how it shows how to use data across targets with App Groups so the data can be shared. :+1:

One issue I had at the end is it has errors saying ā€œCannot find type ā€˜TimelineIntervalIntentā€™ in scopeā€. It builds successfully though and lets the widget be configured. :man_shrugging: (Also the final project does the same thing).

thanks Justin! it seems to be a beta 2 bug. I pulled beta 1 out of the trash and the project builds there without that warning. Hopefully Apple will fix it before GM.

For anyone else getting a bunch of non-specific build errors after adding the Widget target, I was able to solve it by just cleaning the build folder and re-building.

Product ā†’ Clean Build Folder, then build.

Continuing on, there appear to be a few changes to the Starter Code not represented on the tutorial page.

1.

public typealias Entry = SimpleEntry

has been replaced with:

let entry = SimpleEntry

ā€œpublic typealias entryā€ does not appear in the starter code project.

2.

snapshot(with:completion:)

has been replaced with:

snapshot(in:completion:)

General feedback:

1.

First, delete EmitronWidgetEntryView. Youā€™ll use your EntryView instead.

Should specify that youā€™re deleting the EmitronWidgetEntryView within var body: some WidgetConfigruation . When I first read that instruction I was baffled as to why I would be deleting the struct I had just updated during the previous step.

2. I may be doing something wrong, but ā€œPlaceholderViewā€ doesnā€™t appear in my project. Given where it is in the Final project, Iā€™m guessing itā€™s now auto-generated with the title EmitronWidgetEntryView.

3. The StaticConfig init youā€™re using is already deprecated :(

thanks for flagging these Xcode beta updates, Sean!

sorry about the confusion over EmitronWidgetEntryView ā€” that instruction is in the middle of editing EmitronWidget.swift and the two structs have different names, so we didnā€™t think it needed more specification.

Settings does not work with me with message unable to load, I spent a day try to find out what wrong, and then I noticed this ( Make sure its target is both emitron and EmitronWidgetExtension ) :disappointed_relieved:

Thank you for tut! Having build failures building the initial starter app. Project > package loading error > first failure is ā€œFailed to recurse into submodule path 'Tests/Performace/Realmā€. Then 5 issues on emitron target (ā€œPackage resolution errors must be fixed before buildingā€).

In Swift Package Dependencies it looks like all the packages download ok. Iā€™ve tried all the usual cleaning rituals:

  • deleting derived data
  • cleaning build folder
  • reset package caches
  • update to latest package versions

Iā€™m on Xcode 12.2. beta. Any ideas appreciated. Thx!

hi Tarek! apologies, that was a late discovery for us too. Iā€™ll add a screenshot to make it more obvious. Thereā€™s also another instruction on Appleā€™s documentation page that Iā€™ll add, although it didnā€™t seem to make any difference.

When did you last run this project? and which version of Xcode are you using? Because now Iā€™m getting the same build failures as superredviking :confounded:

hi superredviking! I updated the code to 12.2 beta just a week ago and it worked fine. But now itā€™s failing the same as yours. Even in Xcode 12.0. Iā€™ve emailed the error log to the emitron developer but itā€™s midnight where he is. Looking at the errors, it might be something in the 3rd party packages :anguished:

amazing ā€¦ Xcode 12.0.1 installed itself last night, but not in my Applications folder. Itā€™s in the App Store. The project builds fine now. :relieved:

Cannot make the Intent working on my own project. I have widgets showing data from one out of 3 accounts. I wanna allow user to pick account 1, 2 or 3 like you do with timeInterval but it does not work for me.

So I tried exactly your way to see if I could play with time interval refresh but even this does not work on my project.

Anywhere to redirect? Thereā€™s not much out on these widgets yet.

Your tutorial helped tremendously other than the intent. Looking forward to have this fixed or revised.

hi Florent! did you see the instruction about setting the target as both the app and the widget?

select SiriKit Intent Definition File and name it TimelineInterval . Make sure its target is both emitron and EmitronWidgetExtension .

itā€™s non-intuitive but mine wouldnā€™t work until I added the app target

Wow thanks for your quick reply! It was set properly.

I ended going there: Widgets Code-along, part 1: The adventure begins - WWDC20 - Videos - Apple Developer
and it helped (a bit). It shows that your implementation is perfect. No worries!

I then searched a bit more (I hate when this is the fix) I needed to restart xCode for it to finally find my AccountSelectionIntent (it might be worth a note in the tutorial) CharacterSelectionIntent not availā€¦ | Apple Developer Forums

Thanks Audrey for this great tutorial and more importantly your reactiveness.

hi Florent! great that you found a fix! Unfortunately itā€™s something we all get used to, having to restart Xcode or even the Mac. Also Product Clean and delete DerivedData.

This tutorial has been great in learning to integrate widgets into an existing codebase. Everything has worked great but I have come across just a few problems in the final step. Iā€™m using Xcode 12.3.

I am still encountering the ā€œCannot find type ā€˜TimelineIntervalIntentā€™ in scopeā€ error, but this time I cannot build the project at all, as itā€™s now along with a number of other errors that have been introduced at this step.

  1. ā€œType ā€˜Providerā€™ does not conform to protocol ā€˜IntentTimelineProviderā€™ā€
  2. ā€œCannot find type ā€˜TimelineIntervalIntentā€™ in scopeā€
  3. ā€œCannot convert value of type ā€˜WidgetContentā€™ to expected argument type ā€˜Timelineā€™ā€
  4. ā€œReference to invalid associated type ā€˜Entryā€™ of type ā€˜Providerā€™ā€
  5. ā€œCannot find ā€˜configurationā€™ in scopeā€
  6. ā€œInitializer ā€˜init(kind:intent:provider:content:)ā€™ requires the types ā€˜TimelineIntervalIntentā€™ and ā€˜Provider.Intentā€™ be equivalentā€
  7. ā€œCannot find type ā€˜TimelineIntervalIntentā€™ in scopeā€
  8. ā€œCannot convert value of type ā€˜(WidgetContent) ā†’ EntryViewā€™ to expected argument type ā€˜(Provider.Entry) ā†’ EntryViewā€™ā€

The app does not build and therefore I cannot see the user configuration in action, which is really annoying :sweat:

The only other thing I have encountered was that when adding to ContentRepository.swift in the ā€œWriting the Contents Fileā€ step, the breakpoint was never being hit yet the app still built and the widget was working as intended.

Any advice would be greatly appreciated.

hi, Iā€™ve just downloaded the app, uncommented the intent code and run it in a simulator. Everything builds and runs OK. (Xcode 12.3 on Catalina).

Make sure you set the target to both emitron and EmitronWidgetExtension.

I found another tutorial that says: Go to Project Targets, add your intent under Supported Intents. Try this? I didnā€™t need it to run the app.

Also try restarting your Mac