关于 swift:使用 updateApplicationContext 将自定义对象从 Apple Watch 传输到 iPhone | 珊瑚贝

Transter custom object from Apple watch to iPhone using updateApplicationContext


我是 Swift 新手并开始了我的第一个应用程序。

我正在尝试使用 updateApplicationContext 将数据从 Apple Watch 传输到 iPhone,但只收到错误消息:

1
[WCSession updateApplicationContext:error:]_block_invoke failed due to WCErrorCodePayloadUnsupportedTypes

这是我的 WatchKit 扩展中的代码:

1
2
3
4
5
6
7
8
9
10
var transferData = [JumpData]()

func transferDataFunc() {
    let applicationDict = [“data”:self.transferData]
    do {
        try self.session?.updateApplicationContext(applicationDict)
    } catch {
        print(“error”)
    }
}

这些是我要发送的对象结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class AltiLogObject {

let altitude : Float
let date : Date

init (altitude: Float) {
    self.altitude = altitude
    self.date = Date.init()
}

init (altitude: Float, date : Date) {
    self.altitude = altitude
    self.date = date
}

}

class JumpData {

let date : Date
let altitudes : [AltiLogObject]

init(altitudes: [AltiLogObject]) {
    self.altitudes = altitudes
    self.date = Date.init()
}
init(altitudes: [AltiLogObject], date : Date) {
    self.date = date
    self.altitudes = altitudes
}
}

为了完成它,接收函数的代码:

1
2
3
4
5
6
7
8
private func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    let transferData = applicationContext[“data”] as! [JumpData]

    //Use this to update the UI instantaneously (otherwise, takes a little while)
    DispatchQueue.main.async() {
        self.jumpDatas += transferData
    }
}

欢迎任何提示,因为我正在尝试让它运行一个多星期。


您只能通过 updateApplicationContext() 发送属性列表值 – 文档说明了这一点,但根本不清楚。

可以在 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html 中找到您可以发送的对象类型

显然不支持您的自定义对象类型,因此您需要将属性分解为组成部分(例如您的高度和日期属性),将它们单独发送到传递给 updateApplicationContext() 的 applicationContext 字典中然后在接收端重建对象。

这当然是一种真正的痛苦,但这就是这一切的运作方式。

HTH

  • 另一种选择是将对象序列化为数据,然后在另一端反序列化它们。与约翰将它们分解为组成部分的建议没有太大区别。
  • 这可能是比我更好的建议!


来源:https://www.codenong.com/48054216/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8723.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?