Fetching and displaying data on Watch app in SwiftUI

In previous post "Dynamic user notification on Apple Watch with SwiftUI" I was looking into how to add WatchOS app to an existing project and how to create dynamic notifications. This time, I am gonna cover basics how to fetch data from companion iOS app's CoreData storage using WatchConnectivity framework and displaying the data in …

Dynamic user notification on Apple Watch with SwiftUI

Apps which integrate push or local notifications can customise notifications on Apple Watch. Let's go through steps required for adding dynamic notifications on Apple Watch. Sample use case is an app which reminds when a plant needs watering. We'll only concentrate on adding dynamic notification view and leave out sending local notifications from the iOS …

Injecting dependencies using environment values and keys in SwiftUI

Instead of initializing SwiftUI views with dependencies, SwiftUI also offers other ways for injecting dependencies. This time let's take a look on EnvironmentKey which defines a key for inserting objects to environment and vise-versa. We need to create a EnvironmentKey, adding an object to environment and then getting the object in SwiftUI view. Creating EnvironmentKey …

Adding custom attribute to NSAttributedString on iOS

NSAttributedString is used for displaying rich text on Apple platforms. There are many attributes available for styling the text. Let's see how to add new attribute and how to draw it in UITextView. Drawing custom attribute Defining a new attribute is just a matter of extending NSAttributedString.Key and defining a new static variable. Difficult part …

Persistent reusable container for item collections in Swift

Let's build a container where we can store collections of items conforming to a protocol. All the collections are identified by a case in enum. For making the container reusable, we'll use protocols as requirements on keys and items in collections. Moreover, the container should be archivable and unarchivable. Creating a reusable container Container's implementation …

NavigationLink and presentationMode environment value property for dismissing a view in SwiftUI

How to navigate to a new view in SwiftUI and then dismissing it? Let's set up a main view in NavigationView and NavigationLink for opening detail view. NavigationLink is a button triggering a navigation to a specified view. Detail view will contain a button for navigating back to the first view. But how do we …