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

问题描述 投票:1回答:1

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

在Xcode 10.x.x中,WatchKit App的目标始终会触发iOS和Watch App。但是从Xcode 11开始,它只会触发Apple Watch模拟器。我已经仔细检查过使用校正后的配对模拟器(校正后的配对iPhone + Apple Watch模拟器)。已经检查了所有要激活的WCSesssionActivationStateWCSession.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扩展中的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")内部]]

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

ios ios-simulator apple-watch
1个回答
1
投票

对于那些面临相同问题的人。我仍然无法使用wcSession.transferUserInfo发送数据,但它可以与另一个API wcSession.sendMessage一起使用。似乎现在迁移逻辑以使用sendMessage就是解决方法。

© www.soinside.com 2019 - 2024. All rights reserved.