gpt4 book ai didi

swift - WCSession 尚未激活

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:40 24 4
gpt4 key购买 nike

当我尝试发送内容时收到 WCSession 尚未激活错误。而且我不知道我做错了什么。我已经测试了一些“必须”工作的预制解决方案。但它在我的模拟器和物理设备上不起作用。

部分代码:

我的应用委托(delegate):

import UIKit
import CoreData
import WatchConnectivity


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?


func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : String]) -> Void) {
replyHandler(["message": "Hello Watch!"])
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}

func sessionDidBecomeInactive(_ session: WCSession) {

}

func sessionDidDeactivate(_ session: WCSession) {

}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

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

return true
}

........


}

我的接口(interface) Controller :

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController {


override func awake(withContext context: Any?) {
super.awake(withContext: context)

// Configure interface objects here.
}

override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()

if WCSession.default().isReachable {
let messageDict = ["message": "hello iPhone!"]

WCSession.default().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
//print(replyDict)
}, errorHandler: { (error) -> Void in
// print(error)
})
}

}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

}

override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}

扩展委托(delegate):

   import WatchKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
if WCSession.isSupported() {
let session = WCSession.default()
session.delegate = self
session.activate()
}
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

}
....

是否有错误的代码或实现?因为没有什么对我有用。

最佳答案

我想出了我遇到的问题,我认为你的问题也很相似。我试图在调用 activate() 后立即发送消息,这在过去有效,但自从引入 session(activationDidCompleteWith:) 函数后,我需要发送我的消息来自那里。

所以我认为您的解决方案是以下之一:

  1. 让您的 InterfaceController 成为 WCSessionDelegate,移动所有WCSession 在那里激活调用,并从 session (activationDidCompleteWith:)
  2. 发消息自session(activationDidCompleteWith:)ExtensionDelegate 中(在我的应用程序我处理 ExtensionDelegate)
  3. 中的所有通信
  4. session(activationDidCompleteWith:) 中发布通知ExtensionDelegate 并添加一个观察者来处理它接口(interface) Controller

除此之外,我认为您可能还有一个问题,因为您的 AppDelegate 中 session(didReceiveMessage:) 的声明是错误的(或者至少我在尝试您的代码时遇到了问题。 )

新的声明是:

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void)

关于swift - WCSession 尚未激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40707988/

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