AppleWatch 模拟器和 iPhone 模拟器之间的 transferUserInfo 从 Xcode 11 开始停止工作
transferUserInfo between AppleWatch simulator and iPhone simulator stop working from Xcode 11
我有一个依赖手表的应用程序,它在 Xcode 10.2.1 的模拟中运行良好,但是当我更新到 Xcode 11.x.x 时,似乎传输数据不再工作了。
在 Xcode 10.x.x 中,WatchKit App 的目标始终触发 iOS 和 Watch App。但是从Xcode11开始,它只触发Apple Watch模拟器。我已经仔细检查以使用更正的配对模拟器(更正的配对 iPhone + Apple Watch 模拟器)。已经检查了所有要激活的 WCSesssionActivationState
,WCSession.default.isReachable
为真,session didFinish userInfoTransfer
被调用用于同一目标,但在另一个目标中 session didReceiveUserInfo
根本没有被调用.
我还需要做哪些配置吗?有人有同样的问题吗?
非常感谢任何帮助!
这是主应用程序中的代码ViewController
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var textFieldMessage : UITextField!
@IBOutlet weak var buttonSend : UIButton!
var wcSession : WCSession!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
wcSession = WCSession.default
wcSession.delegate = self
wcSession.activate()
}
//MARK: - Button Actions
@IBAction func clickSendMessage(_ sender : UIButton) {
let message = ["message" : textFieldMessage.text!]
do {
try wcSession.updateApplicationContext(message)
if wcSession.activationState == .activated {
if wcSession.isReachable {
let data = ["text": "User info from the iphone"]
wcSession.transferUserInfo(data)
}
}
} catch {
print("Something went wrong")
}
}
// MARK: - WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
}
func sessionDidBecomeInactive(_ session: WCSession) {
print("%@", "sessionDidBecomeInactive: \(session)")
}
func sessionDidDeactivate(_ session: WCSession) {
print("%@", "sessionDidDeactivate: \(session)")
}
func sessionWatchStateDidChange(_ session: WCSession) {
print("%@", "sessionWatchStateDidChange: \(session)")
}
func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) {
DispatchQueue.main.async {
if session.isReachable {
print("Transfered data")
}
}
}
}
和 WatchKit Extension 中的 InterfaceController
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
var session : WCSession?
@IBOutlet weak var sessionLabel : WKInterfaceLabel!
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
session = WCSession.default
session?.delegate = self
session?.activate()
}
// MARK: - WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
NSLog("didReceiveApplicationContext : %@", applicationContext)
sessionLabel.setText(applicationContext["message"] as? String)
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
print("9. InterfaceController: ", "didReceiveUserInfo")
DispatchQueue.main.async {
if let text = userInfo["text"] as? String {
print(text)
}
}
}
}
很奇怪 wcSession.updateApplicationContext(message)
工作正常但是 wcSession.transferUserInfo(data)
不发送数据到苹果手表,甚至代码进入 print("Transfered data")
for ViewController
对于那些面临同样问题的人。我仍然无法使用 wcSession.transferUserInfo
发送数据,但它可以与另一个 api wcSession.sendMessage
一起使用。似乎迁移逻辑以使用 sendMessage 是目前的解决方法。
我有一个依赖手表的应用程序,它在 Xcode 10.2.1 的模拟中运行良好,但是当我更新到 Xcode 11.x.x 时,似乎传输数据不再工作了。
在 Xcode 10.x.x 中,WatchKit App 的目标始终触发 iOS 和 Watch App。但是从Xcode11开始,它只触发Apple Watch模拟器。我已经仔细检查以使用更正的配对模拟器(更正的配对 iPhone + Apple Watch 模拟器)。已经检查了所有要激活的 WCSesssionActivationState
,WCSession.default.isReachable
为真,session didFinish userInfoTransfer
被调用用于同一目标,但在另一个目标中 session didReceiveUserInfo
根本没有被调用.
我还需要做哪些配置吗?有人有同样的问题吗? 非常感谢任何帮助!
这是主应用程序中的代码ViewController
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
@IBOutlet weak var textFieldMessage : UITextField!
@IBOutlet weak var buttonSend : UIButton!
var wcSession : WCSession!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
wcSession = WCSession.default
wcSession.delegate = self
wcSession.activate()
}
//MARK: - Button Actions
@IBAction func clickSendMessage(_ sender : UIButton) {
let message = ["message" : textFieldMessage.text!]
do {
try wcSession.updateApplicationContext(message)
if wcSession.activationState == .activated {
if wcSession.isReachable {
let data = ["text": "User info from the iphone"]
wcSession.transferUserInfo(data)
}
}
} catch {
print("Something went wrong")
}
}
// MARK: - WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
}
func sessionDidBecomeInactive(_ session: WCSession) {
print("%@", "sessionDidBecomeInactive: \(session)")
}
func sessionDidDeactivate(_ session: WCSession) {
print("%@", "sessionDidDeactivate: \(session)")
}
func sessionWatchStateDidChange(_ session: WCSession) {
print("%@", "sessionWatchStateDidChange: \(session)")
}
func session(_ session: WCSession, didFinish userInfoTransfer: WCSessionUserInfoTransfer, error: Error?) {
DispatchQueue.main.async {
if session.isReachable {
print("Transfered data")
}
}
}
}
和 WatchKit Extension 中的 InterfaceController
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
var session : WCSession?
@IBOutlet weak var sessionLabel : WKInterfaceLabel!
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
session = WCSession.default
session?.delegate = self
session?.activate()
}
// MARK: - WCSessionDelegate
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
NSLog("%@", "activationDidCompleteWith activationState:\(activationState) error:\(String(describing: error))")
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
NSLog("didReceiveApplicationContext : %@", applicationContext)
sessionLabel.setText(applicationContext["message"] as? String)
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
print("9. InterfaceController: ", "didReceiveUserInfo")
DispatchQueue.main.async {
if let text = userInfo["text"] as? String {
print(text)
}
}
}
}
很奇怪 wcSession.updateApplicationContext(message)
工作正常但是 wcSession.transferUserInfo(data)
不发送数据到苹果手表,甚至代码进入 print("Transfered data")
for ViewController
对于那些面临同样问题的人。我仍然无法使用 wcSession.transferUserInfo
发送数据,但它可以与另一个 api wcSession.sendMessage
一起使用。似乎迁移逻辑以使用 sendMessage 是目前的解决方法。