gpt4 book ai didi

swift - 我无法获得 WatchConnectivity 的 session :didRecieveMessage: to be called

转载 作者:行者123 更新时间:2023-11-28 09:00:57 24 4
gpt4 key购买 nike

我已尝试让 WatchConnectivity 正常工作,但我无法将(位置)消息从我的 iPhone 发送到我的 Apple Watch。在 AppDelegate 中,我设置了 session :

var session: WCSession!    

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if WCSession.isSupported() {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
return true
}

在我的 ViewController 中:

let manager = CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
self.manager.delegate = self
// CLLocationManager status handling code
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // I know this is called
if let locationData = manager.location {
let location = ["location" : locationData]
(UIApplication.sharedApplication().delegate as! AppDelegate).session.sendMessage(locationData, replyHandler: nil, errorHandler: nil)
}
}

这里是相关的接口(interface) Controller 代码:

var session: WCSession!

override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if WCSession.isSupported() {
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}


func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { // This method is never called
let location = message["location"] as? CLLocation
print(location)
}

但是,我的 InterfaceController 没有收到消息,没有打印任何内容,也没有触发我设置的断点。

谁能指出我正确的方向?谢谢。

最佳答案

传递给sendMessage的字典内容只能包含属性列表类型(基本如数字、字符串、数据等)。我敢打赌,如果您添加了一个错误处理程序,您会收到一条错误消息,指示无法发送有效负载,因为它包含无效(非 plist)类型

此外,当您调用 sendMessage 时,如果您传入一个 nil replyHandler,则在接收方将调用与有 replyHandler 时不同的委托(delegate)方法。尝试实现另一个委托(delegate) didReceiveMessage 方法:

func session(session: WCSession, didReceiveMessage message: [String : AnyObject])

关于swift - 我无法获得 WatchConnectivity 的 session :didRecieveMessage: to be called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32028392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com