iosswiftuserdefaultsappgroups

UserDefaults have different values within same app group


I have App+Extension and I made shared storage like this

extension UserDefaults {
    static let suiteName = "group.myApp"
    static let extensions = UserDefaults(suiteName: suiteName)!

    private enum Keys {
        static let personNames = "personNames"
    }

    var personNames: [String] {
        get {
            guard let array = UserDefaults.extensions.array(forKey: Keys.personNames) as? [String] else {
                return []
            }
            return array
        }
        set {
            UserDefaults.extensions.setValue(newValue, forKeyPath: Keys.personNames)
        }
    }}

But when I debug app I see memory adress A and right values (because I put values through Main app but when I debug extension I have memory addres B and no values inside.

I am using same AppGroup on both Main and Extension app and I access data like this

   let personNames = UserDefaults.extensions.personNames

Is it problem that I am using free Apple Id account? I have possibility to create groups with this account so I doubt.


Solution

  • Ok, I found solution. I made new group with format goup.com.company.appName.sharedStorage and also I use that as suitName and everything was working.

    So maybe because of free apple account there was a problem with creating app groups (because it's automatic) or maybe because group.appName format is invalid.