gpt4 book ai didi

ios - sendMessage 的问题 - WatchConnectivity

转载 作者:搜寻专家 更新时间:2023-11-01 06:40:10 28 4
gpt4 key购买 nike

我一直在为 WCSession 使用 NatashaTheRobot 单例,但无法使 sendMessage 正常工作。

我的目标是从 Watch 应用程序向 iOS 应用程序发送一条消息,并将字典从 iOS 应用程序传输到 watch 应用程序。

这是我在 ExtensionDelegate

中的代码
import WatchKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

var session:WCSession!
var boolCheck = Int()

func applicationDidFinishLaunching() {

WatchSessionManager.sharedManager.startSession()
print("Here i am")
}

func applicationDidBecomeActive() {
print("I AWOKE")
}

func applicationWillResignActive() {

}

}

class WatchSessionManager: NSObject, WCSessionDelegate {

static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}

private let session: WCSession = WCSession.defaultSession()

func startSession() {
session.delegate = self
session.activateSession()
if WCSession.isSupported(){
self.session.sendMessage(["b":"peek"], replyHandler: nil, errorHandler: nil)
print("works")
} else {
print("don't work")
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
let sweetN = message["b"]! as? String
dispatch_async(dispatch_get_main_queue(), {
if sweetN == "insertData1" {
NSNotificationCenter.defaultCenter().postNotificationName("sweetData1", object: nil)
})
}
func sendMessage(message: [String : AnyObject],
replyHandler: (([String : AnyObject]) -> Void)? = nil,
errorHandler: ((NSError) -> Void)? = nil)
{
session.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
print("this is message \(replyHandler)")
var pretty = replyHandler
}

这是我在 iOS 应用程序的 WCSingleton 中的代码(与 AppDelegate 分开)

import WatchConnectivity

@available(iOS 9.0, *)
class WatchSessionManager: NSObject, WCSessionDelegate {

static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}

private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil

private var validSession: WCSession? {

if let session = session where session.paired && session.watchAppInstalled {
return session
}
return nil
}

func startSession() {
session?.delegate = self
session?.activateSession()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//receieve messages from watch
print(message["b"]! as? String)
let sweetN = message["b"]! as? String
dispatch_async(dispatch_get_main_queue(), {
if sweetN == "peek"{
NSNotificationCenter.defaultCenter().postNotificationName("giveMeInfo", object: nil)
}
}
})
}

@available(iOS 9.0, *)
extension WatchSessionManager {

func sendMessage(message: [String : AnyObject],
replyHandler: (([String : AnyObject]) -> Void)? = nil,
errorHandler: ((NSError) -> Void)? = nil)
{
session!.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
}

}

这是我在 ViewController 中使用的方法(从 NSNotificationCenter 触发)。然而,这部分代码永远不会被执行(这很奇怪,因为当我使用 applicationContext 时它工作得很好)。

func giveMeInfo(){
let linesAdd1 = linesAdd as! AnyObject
WatchSessionManager.sharedManager.sendMessage(["a":linesAdd1])
}

非常欢迎任何关于如何让所有这些部分协同工作的见解!

最佳答案

你的代码对我来说有点困惑,看起来不错,但由于以下原因,你会在使用这种方法时遇到麻烦:

  • 如果您在 ViewController 中收到通知并且 Watch 变为非事件状态,则 sendMessage() 方法将无法发送回数据:

    Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable. Calling this method from your iOS app does not wake up the corresponding WatchKit extension. If you call this method and the counterpart is unreachable (or becomes unreachable before the message is delivered), the errorHandler block is executed with an appropriate error. The errorHandler block may also be called if the message parameter contains non property list data types.

  • 如果你想取回数据,那么你应该使用回复 block 。但在您的配置中,这些 block 将不会被调用,因为:

sendMessage(reply==nil) --> didReceiveMessage(... message: )

sendMessage(reply!=nil) --> didReceiveMessage(... message: replyHandler:)

  • 另一方面,如果您使用 contextMethod:

    Use this method to transfer a dictionary of data items to the counterpart app. The system sends context data when the opportunity arises, with the goal of having the data ready to use by the time the counterpart wakes up. The counterpart’s session delivers the data to the session:didReceiveUpdate: method of its delegate. A counterpart can also retrieve the data from the receivedApplicationContext property of its session.

我希望这对您有所帮助;)

关于ios - sendMessage 的问题 - WatchConnectivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519863/

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