App group not working between iOS and watchOS

Hi everyone, I'm using an app group to share data between iOS and it's watch companion app. I ensured that is has the same identifier in Signing & Capabilities and in the .entitlements files. Here is the UserDefaults part:

class UserDefaultsManager {

private let suitName = "group.com.sanjeevbalakrishnan.Test"

public func saveItems(_ items: [ItemDTO]) {
    print("Save \(items.count) items to shared defaults")
    let defaults = UserDefaults(suiteName: suitName)
    let data = try? JSONEncoder().encode(items)
    defaults?.set(data, forKey: "items")        
}

public func loadItems() -> [ItemDTO] {
    let defaults = UserDefaults(suiteName: suitName)
    print(defaults)
    guard let data = defaults?.data(forKey: "items") else {
        print("watchOS received data is empty")
        return []
    }
    
    let items = [ItemDTO].from(data: data)
    print("Load \(items.count) items from user defaults")
    return items
}

}

For testing I called loadItems after saveItems on iOS app and it returned items. However, on watchOS app it always returns empty array. What do I need to consider? Thanks.

Best regards Sanjeev

Answered by blacksun in 856323022

App Group is device level, iPhone and Apple Watch are two devices.

You can consider WCSession.

App Group is device level, iPhone and Apple Watch are two devices.

You can consider WCSession.

Accepted Answer

Right. I have a more detailed explanation of this here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Ok, thank you!

App group not working between iOS and watchOS
 
 
Q