我正在使用模拟器,因为我没有 Apple Watch 来测试它。我的应用程序“App Delegate”中有这段代码。
import UIKit
import CoreData
import Parse
import WatchConnectivity
@available(iOS 9.0, *)
extension AppDelegate : WCSessionDelegate {
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject])
{
}
// Sender
@available(iOS 9.0, *)
func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer? {
return WCSession.defaultSession().transferUserInfo(userInfo)
}
@available(iOS 9.0, *)
func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
// implement this on the sender if you need to confirm that
// the user info did in fact transfer
}
@available(iOS 9.0, *) // Receiver
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 9.0, *) {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
WCSession.defaultSession().transferUserInfo(["hello" : "Hey Alvin Varghese"])
}
} else {
}
}
}
如你们所见,我正在通过“userInfo”发送一个虚拟数据。并在我的 Apple Watch 接口(interface) Controller 中成功接收到它。代码如下。
import WatchKit
import Foundation
import WatchConnectivity
@available(iOS 9.0, *)
extension StartingPageInterfaceController : WCSessionDelegate {
// Sender
@available(iOS 9.0, *)
func transferUserInfo(userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer? {
return WCSession.defaultSession().transferUserInfo(userInfo)
}
@available(iOS 9.0, *)
func session(session: WCSession, didFinishUserInfoTransfer userInfoTransfer: WCSessionUserInfoTransfer, error: NSError?) {
// implement this on the sender if you need to confirm that
// the user info did in fact transfer
}
@available(iOS 9.0, *) // Receiver
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
dispatch_async(dispatch_get_main_queue()) {
if let hey = userInfo["hello"] as? String
{
let alert : WKAlertAction = WKAlertAction(title: "Okay", style: WKAlertActionStyle.Default, handler: {
})
self.presentAlertControllerWithTitle("Okay", message: hey, preferredStyle: WKAlertControllerStyle.Alert, actions: [alert])
}
}
}
}
class StartingPageInterfaceController: WKInterfaceController {
@IBOutlet var searchButton: WKInterfaceButton!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if #available(iOS 9.0, *) {
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
} else {
}
}
}
好的,现在我在我的 iPhone 6 模拟器中运行它,我可以看到安装成功。现在我转到 Apple Watch 32mm Simulator 并单击刚刚安装的应用程序,打开并显示这样的警告消息 --<“Okay Hello Alvin Varghese”(当父应用程序在 iPhone 6 模拟器中打开时。)。现在我直接将 Apple Watch 应用程序安装到 Apple Watch 32mm 模拟器中,但没有任何反应,我期待着相同的警告消息(虽然父应用程序未在 iPhone 6 模拟器中打开)。
如果我在 Apple Watch 32mm 模拟器中运行我的 Apple Watch 应用程序并立即打开父项,我可以看到警告消息。那么这里发生了什么??我读到 Watch Connectivity 框架将在后台进行通信。
所以从这里我可以看出这仅在父设备处于事件状态时才有效,还是因为我正在模拟器上测试它?它会在物理设备中正常工作吗??
我的问题很简单,当我打开 Apple Watch 时,我想要来自父应用程序的数据。我怎样才能做到这一点??
让我知道您的想法,在此先感谢您。
最佳答案
首先,假设您想在名为 infoNeeded
的实例变量中获取所需的信息。您可以像这样在您的类(class)中声明它(例如)
var infoNeeded : [String : AnyObject]?
现在,当您希望获得所需的信息时(例如,在 WatchOS 扩展的 ExtensionDelegate 中的 applicationDidBecomeActive
方法中),您需要从 watch 向 iPhone 发送一条消息.此外,您还必须为可能从 iOS 收到的包含您需要的数据的响应做好准备。为此,你做
let replyHandler: ([String : AnyObject]) -> Void = { reply in
self.infoNeeded = reply
}
let msg = ["InfoType" : "MainInfo"] // This must be something that allows the iOS WCSessionDelegate to know what info it must provide in the reply
WCSession.defaultSession().sendMessage(msg, replyHandler:replyHandler, errorHandler:nil) // you can pass an error handler if you wish
然后,在 iOS 中的 WCSessionDelegate 中(在您的情况下,您的 AppDelegate),您必须实现该功能
func session(_ session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler replyHandler: ([String : AnyObject]) -> Void) {
let data: [String : AnyObject] = <whatever info you need to pass>
replyHandler(data)
}
有了这个,你得到的是 watch 中的变量 infoNeeded
填充了在 iOS 中创建的 data
。
关于ios - 观看连接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522673/