问题描述
我正在尝试将一个字符串从我的 Apple Watch 传递到 iPhone,但它似乎没有连接.这是我的代码:
I'm trying to pass a String from my Apple Watch to an iPhone but it seems like it's not connecting. Here's my code:
ViewController.swift :
ViewController.swift :
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var lablel: UILabel!
var string = "Hello World"
let session = WCSession.default()
override func viewDidLoad() {
super.viewDidLoad()
session.delegate = self
session.activate()
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
let msg = message["StringValueSentFromiWatch"] as! String
lablel.text = "Message : \(msg)"
print("iphone recieved message")
}
func session(_ session: WCSession,
activationDidCompleteWith activationState: WCSessionActivationState,
error: Error?) {
}
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
}
InterfaceController.swift :
InterfaceController.swift :
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
let session = WCSession.default()
override func willActivate() {
super.willActivate()
session.delegate = self
session.activate()
}
@IBAction func SendPressed() {
//Send Data to iOS
let msg = ["StringValueSentFromiWatch" : "Hello World"]
session.sendMessage(msg, replyHandler: { (replay) -> Void in
print("apple watch sent")
}) { (error) -> Void in
print("apple watch sent error")
}
}
func session(_ session: WCSession,
activationDidCompleteWith activationState: WCSessionActivationState,
error: Error?){
}
}
我正在尝试向 iPhone 发送Hello World",但我在控制台中收到了此打印输出:
I'm trying to send "Hello World" to the iPhone but I get this printout in the console:
errorHandler: 是 WCErrorCodePayloadUnsupportedTypes
和Apple Watch 发送错误".
and 'apple watch sent error'.
我知道它没有发送,但我不知道为什么.有谁知道为什么这不起作用?
I know it's not sending but I don't know why. Does anyone know why this doesn't work?
注意:我运行的是模拟器,但我很确定这不是问题.
Note: I'm running this is the simulator but I'm fairly sure this is not the problem.
推荐答案
我认为您在 sendMessage() 中搞砸了,我无法计算出 replyHandler 语法,并且您错过了 errorHandler: 参数.
I think you have messed up in the sendMessage(), I cannot work out the replyHandler syntax, and you miss the errorHandler: parameter.
无论如何,我已经尝试了你的代码,只要稍作改动就可以了.
Anyway, I've tried your code, and with a few changes it would work.
1).在 InterfaceController 中,sendPressed():
1). In InterfaceController, the sendPressed():
var count = 0
@IBAction func SendPressed() {
//Send Data to iOS
let msg = ["Count" : "\(count)"]
if session.isReachable {
session.sendMessage(msg, replyHandler: nil, errorHandler: { (error) -> Void in
print("Error handler: \(error)")
})
count += 1
}
}
我添加了一个计数,因为每次通话的消息都必须有所不同(以节省电池电量),因此您现在可以连续多次按下按钮.并检查以验证主机应用程序是否可访问.
I've added a count, since the message must vary for each call (to conserve battery), so you can now press the button several times in a row. And a check to verify that the host application is reachable.
2.) 在 ViewController 中,记得在主线程更新 GUI:
2.) In the ViewController, remember to update the GUI on the main thread:
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
DispatchQueue.main.async {
self.lablel.text = "Message : \(message)"
}
}
否则标签不会在您收到数据时更新.
Otherwise the label will not update when you receive the data.
如果对您有帮助,请告诉我!
Let me know if it helps you!
这篇关于Apple Watch 不向 iPhone 传递数据 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!