Search code examples
iosiphonearraysswiftwatchos-2

Is it possible to send complex Arrays from iPhone to AppleWatch with watch connectivity?


I have a problem passing data from iOS to WatchOS 2.0

I want to send an ArrayList to WatchOS but my ArrayList has no type like String, Int but an Object that I generated.

    // here I fetch my Lists with Users
    var friendList: [UserProfile] = Utils().loadUsers("friendList")
    var blackList: [UserProfile] = Utils().loadUsers("blackList")
    var users: [UserProfile] = Utils().loadUsers("UsersList")

    // here I put the Lists in the Dictionary in order to send this Dictionary to Watch
    let dictionary: [String: AnyObject]=[
        "UsersList" : self.users,
        "BlackList" : self.blackList,
        "FriendList" : self.friendList
    ]

    WCSession.defaultSession().sendMessage(dictionary, replyHandler: { (data) -> Void in
        // handle the response from the device

        }) { (error) -> Void in
            print("error: \(error.localizedDescription)")
    }

In my WatchApp Class I try to get the Data but there is following error:

error: Payload contains unsupported type.

This is how I want to get the Data. If I send Bools, Integers or String this works, but not for Arrays like mine:

let userList: [UserProfile] = applicationContext["UsersList"] as! [UserProfile]
let blackList: [UserProfile] = applicationContext["BlackList"] as! [UserProfile]
let friendList: [UserProfile] = applicationContext["FriendList"] as! [UserProfile]

Hope anyone can help me with this Problem.


Solution

  • UserProfile objects friendList, blackList, users are not serialised yet, and cannot be directly send to Apple Watch.

    You can convert them to dictionaries before sending them to the Apple Watch.