是否可以通过手表连接将复杂数组从 iPhone 发送到 Apple Watch?

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

我在将数据从 iOS 传递到 WatchOS 2.0

时遇到问题

我想发送 ArrayListWatchOS 但我的 ArrayList 没有像 StringInt 这样的类型但是我生成的 Object

    // 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)")
    }

在我的 WatchApp Class 中,我尝试获取数据但出现以下错误:

error: Payload contains unsupported type.

这就是我想要获取数据的方式。如果我发送 BoolsIntegersString 这有效,但不适用于像我这样的 Arrays

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

希望有人能帮我解决这个问题。

UserProfile对象friendList、blackList、users尚未序列化,无法直接发送到Apple Watch。

您可以在将它们发送到 Apple Watch 之前将它们转换为字典。

您可以使用 属性 列表中允许的任何类型:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html

由于 NSString 是其中一种数据类型,您可以使用任何可以序列化为字符串的数据类型。

示例:您可以将对象序列化为 JSON 字符串,将其作为字符串发送到手表并使用 JSON 从中创建对象。我就是这样选择的。

Step 1: You need to have NSCoding properly working.

import Foundation

class UserProfile : NSObject, NSCoding {

    /// The name of the activity.
    var name: String

   /**
    The constructor

    :param: name The name of the user.
    */
    init(name: String, start: Int, end: Int) {
        self.name = name
    }

    required init(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObjectForKey("Name") as! String
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(name, forKey: "Name")
    }
}

Step 2: Set the function that will be receiving your data:

func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void) {

        // Set the same class to avoid the name change for every target.
        NSKeyedUnarchiver.setClass(UserProfile.self, forClassName: "UserProfile")

        // Unarchive the activity object passed from the paired device.
        guard let data = NSKeyedUnarchiver.unarchiveObjectWithData(messageData) else {
            return
        }

        let userProfiles = data as! [UserProfile]

        //Send the replyHandler you might need
        let response: NSData = //...
        replyHandler(response)
    }
}

Step 3: Set the function that will be sending your data:

        let userProfiles: [UserProfile] = //some of your UserProfiles...
        // Set the same class to avoid the name change for every target.
        NSKeyedArchiver.setClassName("UserProfile", forClass: UserProfile.self)

        // Archive the object to NSData.
        let data = NSKeyedArchiver.archivedDataWithRootObject(userProfiles)

        session.sendMessageData(data, replyHandler: { (data) -> Void in
            // handle the response from the device

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

如果 Watch 不更新用户的 ArrayList,最好使用 UserDefaults:-


您可以通过在应用程序和监视目标上启用组的功能并通过 Userdefaults 共享您的“ArrayListObj”目标(iPhone 和观察)。

//iPhone 共享用户信息 func sharedUserInfo() {

    if let userDefaults =  UserDefaults(suiteName: "group.watch.app.com" ) {
        userDefaults.set( UserProfileObj as AnyObject, forKey: "UserInfo")
        userDefaults.synchronize()
        }

    }

//手表提取信息

func sharedInfo() {
        if let userDefaults = UserDefaults(suiteName: "group.watch.app.com") {
            let userInfo = userDefaults.string(forKey: "UserInfo")
           }

       }