How To Change Suite Name For UserDefaults In SwiftUI

As there is a new property wrapper in SwiftUI for UserDefaults called AppStorage. There is a need to be able to set a new suite name for it.

Let’s take code from using AppStorage.

@AppStorage(“ButtonTapsCount”) private var tapsCount = 0

VStack {
Text(“Taps count: \(tapsCount))
Button(“Tap me!”) {
    tapsCount += 1

On each click tapsCount increment by one. But this won’t work when you want to share it with some extension service. If you want to share some value you need the same suite name for both.

To change it, you need to pass it inside the property wrapper.

@AppStorage(“ButtonTapsCount”, store: UserDefaults(suiteName: “”)) private var tapsCount = 0        

When you run code, you’ll notice that the previous value disappeared. It happened because you used different storage.

The solution to keep it in both places you need to rewrite it to the new one.