Apple Watch无法将数据传递到iPhone-Swift [英] Apple Watch Not Passing Data to iPhone - Swift
问题描述
我正在尝试将String从我的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?){
}
}
我正在尝试将"Hello World"发送到iPhone,但是我在控制台中得到了此打印输出:
I'm trying to send "Hello World" to the iPhone but I get this printout in the console:
errorHandler:是,使用WCErrorCodePayloadUnsupportedTypes
errorHandler: YES with WCErrorCodePayloadUnsupportedTypes
和苹果手表发送错误".
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!