Hallo vrienden, Leo hier. Today we will talk about how to update or refresh Widget and Siri Intent programmatically in your iOS codebase.

This will be a very short post. I’ve spent some time working on the new lock screen widgets lately, and they are easy to do but not so easy to maintain. Debugging widgets is very painful if you have a dynamic configuration on them.

Widgets with dynamic configuration use the same structure that Siri, and it is called Custom Intents. To make the custom information easily accessible, widgets and Siri can provide customizable properties. For example, users can select a specific food for a food quote widget on the lock screen, or enter a flight tracking number for a flight they are waiting for. Widgets define customizable properties by using custom intent definitions, the same mechanism that Siri Suggestions and Siri Shortcuts use for customizing the custom interactions.

Until iOS 16 was impossible to say to the app that the intent configuration had changed programmatically. We were hostages of the iOS when it would update the intent configuration data. But now, everything has changed. You will never be a hostage of the system anymore when loading widgets with a custom configuration.

Before the code solution…

 

Painting of the Day

The painting I chose today is an 1853 painting called Le Stryge by the master Charles Meryon. Lived from 23 November 1821 to 13 February 1868. He was a French artist who worked almost entirely in etching, as he suffered from color blindness. Although now little-known in the English-speaking world, he is generally recognised as the most significant etcher of 19th century France. He also suffered from mental illness, dying in an asylum. His most famous work is a series of views of Paris.

I chose this painting because that is the face I did when I discovered this new iOS 16 API.

 

 The Problem – Update or Refresh Widget and Siri Intent Programmatically

To do that is very simple. And remember this is just for intents in WatchOS now.

From anywhere in your code you just need to call the code below.

WidgetCenter.shared.invalidateConfigurationRecommendations()

As I said before this API is only available in iOS 16 or higher. This way if your minimal target is not iOS 16, and I think very few apps in the App Store, you probably would have to do something like the code below to refresh Widget and Siri Intent programmatically.

if #available(iOS 16, *) {
    WidgetCenter.shared.invalidateConfigurationRecommendations()
}

And that’s it!

 

Other Curious Topics in iOS Development

If you liked this content you will probably like how to expand your API capabilities with Opaque types. There I explain how Swift ensures runtime safety and the capability to hide properties from users of your APIs at the same time.

Talking about functions, did you know that you can postpone the evaluation of a function parameter until it is really necessary using the autoclosure annotation? Check it now how you can do it!

 

Summary – Updating Widget/Siri Intent Information and Data

Today was a very brief post because I just wanted you to know about this new API. From now on, in your iOS 16 targets, you will never have to wait until the system updates and refresh your custom intent configuration, you can do it yourself!

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for reading and… That’s all folks.

Credits:

title image