10 Tips on Developing iOS 14 Widgets

10 Tips on Developing iOS 14 Widgets

Discover more about Widgets and WidgetKit on iOS 14 using Swift and SwiftUI with 10 tips covering many use cases and hidden rules.

Widgets have been one of the top features of iOS 14 during WWDC 2020, where the biggest change ever of iOS home screen has been unveiled. On the user point of view, they represent a new type of interaction, a new entry point for apps; on a technology point of view, they are a manifesto of the way chosen by Apple, where SwiftUI (the only way to build widget’s views) and an optimized universality (widgets are available on iOS, iPadOS and macOS) are key elements. 

After experimenting with the WidgetKit framework on iOS 14 and Xcode 12 betas, I want to share 10 interesting tips that could be useful for many use cases when coding widgets for your apps. Disclaimer: I assume you already know how to create a simple widget and basic related APIs. Let’s start! 👨🏼‍💻

1. UserDefaults Suite

Very likely, you’ll need to use UserDefaults from your widgets, in order to read user preferences or some other small amount of data. To do so, the standard container isn’t the way, because its content isn’t shared between different targets; instead, as for any other app extension, you have to rely (from the app and from widgets) to the shared “suite”, named with the App Group ID, using the dedicated api:

let userDefaults = UserDefaults(suiteName: “your-app-group-id”)

The App Group is a capability available in the “Signin & Capabilities” tab of your targets: remember to add the App Group capability both in the app’s target and in the widget’s target, otherwise it won’t work.

2. Location permission

As a general rule, widgets inherit every permission status from the parent app and it’s not allowed to prompt requests from the widget itself. Location permission represents an exception: starting from beta 5, when a widget using location is added to the home screen, the user will be automatically_requested to grant permission through an alert; the choice can be changed anytime from the Settings, as a new _“While Using the App or Widgets” option is available.

Image for post

In addition, remember to put the NSWidgetWantsLocation (boolean) and NSLocationUsageDescription keys in the widget extension Info.plist to get location services work properly.

Image for post

Image for post

3. Maps with MKMapSnapshotter

Talking about location, another question could come up: what is the best way to present a map view inside a widget? The recommended option is to use a MKMapSnapshotter in place of a MKMapView; in fact, the standard map view doesn't work at all. The snapshot captures the map’s contents in a simple image. To get the desired MKMapSnapshotter.Snapshot:

The closure is perfect to be used in the getTimeline(in:completion:)function of TimelineProvider protocol: once the image is loaded, you can create your entries and define the timeline. The snapshot is highly customizable and you find the full documentation here.

swift ios swiftui xcode widget

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

HOW TO BUILD CUSTOM ALERT DIALOG BOX FROM SCRATCH STEP BY STEP USING SWIFT iOS XCODE 11.4

Topic: HOW TO BUILD CUSTOM ALERT DIALOG BOX FROM SCRATCH STEP BY STEP USING SWIFT iOS XCODE 11.4 Subscribe Us: http://bit.ly/2UaSC5s Find us on: Discord: htt...

The ultimate guide to iOS Unit Testing with Swift and Xcode

Unit testing is a testing method where you can test “unit” of code whether it is working as you want or not. In Xcode, use XCTest…

SwiftUI 2.0 Custom Tab View Page Icons - iOS 14 Menu's - Xcode 12 - SwiftUI 2.0 Tutorials

In this Video i'm going to show how to create Custom Icon For Tab View Page Style And iOS 14 Menu's Using SwiftUI 2.0 | SwiftUI 2.0 Menu's | SwiftUI 2.0 iOS 14 Menu's | SwiftUI 2.0 Custom Tab View.

Accessibility in iOS 14 Widgets With SwiftUI

This summer, Apple announced widgets for iOS 14 that you can have on your home screen alongside your existing apps. Widgets help you to personalise the screen with glanceable relevant data upfront. Developers must use Apple's WidgetKit and declarative framework SwiftUI for creating the widgets on iOS 14.

Writing Your First Widget for iOS

Getting started with WidgetKit Writing Your First Widget for iOS