gpt4 book ai didi

watchkit - WatchConnectivity transferFile 问题( watch -> iphone)

转载 作者:行者123 更新时间:2023-12-03 20:58:31 28 4
gpt4 key购买 nike

我想使用 WatchConnectivity 和设置 WCSession 将在 watch 上创建的文件发送到 iOS 配套应用程序,但它无法通过 iPhone。当我改为使用发送包含字典的消息时,数据确实会到达 iPhone。

AppDelegate 和 ExtensionDelegate 都使用 NotificationCenter 与 ViewController 和 ExtensionController 进行通信。为简单起见,它们被省略了,但通知工作正常。

iOS 的 AppDelegate.swift

extension AppDelegate: WCSessionDelegate {
// 1
func sessionDidBecomeInactive(_ session: WCSession) {
print("WC Session did become inactive")
}

// 2
func sessionDidDeactivate(_ session: WCSession) {
print("WC Session did deactivate")
WCSession.default.activate()
}

// 3
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: \(error.localizedDescription)")
return
}
print("WC Session activated with state: \(activationState.rawValue)")
}

func setupWatchConnectivity() {
// 1
if WCSession.isSupported() {
// 2
let session = WCSession.default
// 3
session.delegate = self
// 4
session.activate()
}
}

func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {
print("didFinish fileTransfer")
}

func session(_ session: WCSession, didReceive file: WCSessionFile) {
print("didReceive file")
}


func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { // 2
print("didReceiveMessage reached")
}
}

在 watch 上我有:

ExtensionDelegate.swift
extension ExtensionDelegate: WCSessionDelegate {

func setupWatchConnectivity() {
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
}

func session(_ session: WCSession, activationDidCompleteWith
activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print("WC Session activation failed with error: " +
"\(error.localizedDescription)")
return
}
print("WC Session activated with state: " +
"\(activationState.rawValue)")
}

func sendFileToPhone(_ notification:Notification) {
// 1
if WCSession.isSupported() && WCSession.default.isReachable {
// 2
if let fileURL = notification.object as? URL {
print("Sending file for URL: \(fileURL.absoluteURL.absoluteString)")
WCSession.default.transferFile(fileURL, metadata: nil) // <- if I use sendMessage here stuff works...
}
}
}

func session(_ session: WCSession, didFinish fileTransfer: WCSessionFileTransfer, error: Error?) {
// handle filed transfer completion
print("File transfer complete")
print("Outstanding file transfers: \(WCSession.default.outstandingFileTransfers)")
print("Has content pending: \(WCSession.default.hasContentPending)")
}

func session(_session: WCSession, didFinishFileTransfer fileTransfer: WCSessionFileTransfer, error: NSError?) {
print("error: ", error as Any)
}
}

从 watch 发送文件后,我检查,如您所见, outstandingFileTransfershasContentPending WCSession 的属性,并且它们表明该文件应该已被传输,但我的 :didReceive:我的 AppDelegate 扩展的方法没有被调用。同样,当我使用 sendMessage 时,AppDelegate 的 :didReceiveMessage:按预期调用。

我错过了什么?

注意:我试过运行这个 Apple's demo project它具有不同的通信方法,我遇到了同样的事情:transferFile 不会触发对方的任何事情。

最佳答案

这似乎是 iOS 13/watchOS 6 模拟器的错误。我尝试将 Xcode 模拟器更改为 iOS 12 和 watchOS 5,this thread 中的建议问题消失了。

关于watchkit - WatchConnectivity transferFile 问题( watch -> iphone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59575166/

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