gpt4 book ai didi

ios - 向 Apple Watch 发送消息时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 16:01:03 26 4
gpt4 key购买 nike

我在 iOS 应用程序中有一个 UIButton 操作 (sendMessageToWatch),在 Apple Watch 中有一个标签 (messageLabel)。我想要做的是能够点击 iPhone 中的按钮并在 Apple Watch 的标签中看到它。

出于某种原因,当我点击按钮时,应用程序崩溃但没有输出任何错误。

我错过了什么?

这是代码。

iOS View Controller

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

var session: WCSession!

override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
let session = WCSession.default()
session.delegate = self
session.activate()
}
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {}
func sessionDidDeactivate(_ session: WCSession) { }
func sessionDidBecomeInactive(_ session: WCSession) { }

@IBAction func sendMessageToWatch(_ sender: Any) {
//Send Message to Watch
let messageToSend:Dictionary = ["messageFromPhone":"Hello Watch."]

session.sendMessage(messageToSend, replyHandler: { replyMessage in
// some replay
}, errorHandler: {error in
// catch any errors here
})
}

}

观看接口(interface) Controller

import WatchKit
import Foundation
import WatchConnectivity

class InterfaceController: WKInterfaceController, WCSessionDelegate {

var session: WCSession!
@IBOutlet var messageLabel: WKInterfaceLabel!

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
//handle received message
let value = message["messageFromPhone"] as? String
DispatchQueue.main.async {
self.messageLabel.setText(value)
}
}

override func awake(withContext context: Any?) {
super.awake(withContext: context)
}

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.default()
session.delegate = self
session.activate()
}
}

override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {}
}

最佳答案

您正在 viewDidLoad 中声明另一个 session 对象,并用 let 重新声明它。只需删除它:

var session: WCSession!
override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
session = WCSession.default()
session.delegate = self
session.activate()
}
}

关于ios - 向 Apple Watch 发送消息时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41082324/

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