gpt4 book ai didi

ios - AppleWatch 模拟器和 iPhone 模拟器之间的 TransferUserInfo 从 Xcode 11 停止工作

转载 作者:行者123 更新时间:2023-12-01 22:07:14 25 4
gpt4 key购买 nike

我有一个依赖的 watch 应用程序,可以在 Xcode 10.2.1 的模拟中正常工作,但是当我更新到 Xcode 11.x.x 时,传输数据似乎不再起作用。

在 Xcode 10.x.x 中,WatchKit 应用程序的目标始终触发 iOS 和 Watch 应用程序。但从 Xcode 11 开始,它只触发 Apple Watch 模拟器。我已经仔细检查以使用更正的配对模拟器(更正的配对 iPhone + Apple Watch 模拟器)。已检查所有要激活的 WCSessionActivationStateWCSession.default.isReachable 为 true,为同一目标调用 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扩展中的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) 不向 Apple Watch 发送数据,即使代码已发送ViewController 的 print("Transfered data")

最佳答案

对于那些面临同样问题的人。我仍然无法使用 wcSession.transferUserInfo 发送数据,但它可以与另一个 api wcSession.sendMessage 配合使用。看来迁移逻辑以使用 sendMessage 是目前的解决方法。

引用:https://forums.developer.apple.com/thread/127460

关于ios - AppleWatch 模拟器和 iPhone 模拟器之间的 TransferUserInfo 从 Xcode 11 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491891/

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