Demystifying Build Settings and Build Configurations in Xcode In the next section, you’ll explore build settings in Xcode. You can see the widget shows the added record now. Add Leonardo again!Ĭhange the active scheme to WidgetExtention. The record you added isn’t there anymore because you’re using a different UserDefaults suite. This lets the widget access the hatchling data.Ĭhange the active scheme to NinjaCounter. With this code, you ensure the app saves and reads data from the UserDefaults suite that the app group shares. Static private let defaults = UserDefaults( Open UserDefaultsHelper.swift and replace the declaration of defaults with: Now that you’ve created your App Group and added your targets to it, it’s time to let the app group access the UserDefaults suite. This allows data sharing between the host app and the widget extension. widget, but use the same app group that you created for the main app. Now, perform the same steps to the Widget Extension target. Now that you’ve added App Groups to the capabilities, click the + button to create an app group. Remember this as you’ll need again momentarily.Ĭlick + Capability. Open the Signing & Capabilities tab.Ĭhange the Bundle Identifier to something unique to you such as. Select the NinjaCounter project in the Project navigator to show the Project Editor. To solve this, you’ll add the app and widget to an app group. That’s because the extensions don’t have access to the app’s UserDefaults. Build and run.Ĭurrently, the widget doesn’t show any data, even though you just recorded Leonardo. In getTimeline(in:completion:), the widget uses UserDefaultsHelper‘s getRecordsCount() and getRecords() to get the data from UserDefaults. It creates a simple widget that shows the number of hatchlings counted and the tag of the last hatchling reported. Open Widget.swift and take a look at the code. Now that you get the gist of the starter project, you’ll set up the app widget. With that, you created a new record with the hatchling tag and hatch time.
0 Comments
Leave a Reply. |