6

Hopefully a simple question, but I'm wondering how I would access UserDefaults data saved in my UIKit app from a SwiftUI Widget? I need to display some of this data in a widget.

Thanks!

3
  • You need to create an app group Commented Sep 26, 2020 at 15:11
  • Yes, I’ve got an app group, but it’s the part of actually accessing the data that’s an issue for me. I don’t think it’s as simple as defining UserDefaults.standard as in my actual app. Thanks though. Commented Sep 26, 2020 at 16:18
  • See Share data between main App and Widget in SwiftUI for iOS 14 or Sharing Data with AppGroup Commented Sep 28, 2020 at 16:39

1 Answer 1

10

You need to use UserDefaults(suiteName:) instead of UserDefaults.standard along with an AppGroup. UserDefaults.standard is only accessible in the app that it is in, it is not available to any of the extensions or other apps that you may make. This is why you have to use an AppGroup.

Once you have created your AppGroup (you can do this in the Signing and Capabilities section) you should have a suiteName for it, something like:

group.com.my.app.identifier

Then in your UIKit part of your app you can set the values in the AppGroup's UserDefaults in the following way:

if let userDefaults = UserDefaults(suiteName: "group.com.my.app.identifier") {
    userDefaults.setValue("value to save", forKey: "Key")
}

And reading them back you can use:

if let userDefaults = UserDefaults(suiteName: "group.com.my.app.identifier") {
    let value = userDefaults.string(forKey: "Key")
}

As the Widget will be written in SwiftUI you can use the property wrapper @AppStorage to access the values:

@AppStorage("Key", store: UserDefaults(suiteName: "group.com.my.app.identifier"))
var value: String = ""

If you have already stored the values that you wish to use in UserDefaults.standard you will need to copy them across to the UserDefaults(suitName:).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Again, probably quite simple, but I wanted to be sure it's right, how would I transfer values from UserDefaults.standard to the suite? :)
There is no straightforward way to do it. The easiest solution is to just save all the values that you need to be shared exclusively in the suite. If you have the values in standard then you will have to copy them across (probably at app launch). The only way I know to copy is to read from standard and save in the suite. You should also make sure that any defaults that you need in multiple places are updated to use the suite so you don’t have to perform the copy operation again.
Bear in mind that the @AppStorage wrapper will not update the view. The value will be read once but never updated as views in Widgets are static and refreshed by Provider only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.