我正在开发一个在手表和 iOS 父应用程序之间进行通信的应用程序。它通过打开 WatchKit 扩展将数据发送到父应用程序。我了解openParentApplication:reply
,调用时会从 Apple Watch 打开 iPhone 应用程序。之后,application:handleWatchKitExtension:reply
在应用程序的委托中调用。
从那里你可以打开一个通知到视图控制器:
NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?)
“aName”是您可以在视图控制器中打开的内容:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("handleWatchKitNotification:"),
name: "WatchKitSaysHello",
object: nil)
这是我的 WatchKit 扩展中的接口控制器的代码:
//
// InterfaceController.swift
// SendColors WatchKit Extension
//
// Created by Tommy on 12/30/14.
// Copyright (c) 2014 Tommy. All rights reserved.
//
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
var ypo = false
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
@IBOutlet weak var redButton: WKInterfaceButton!
@IBOutlet weak var greenButton: WKInterfaceButton!
@IBOutlet weak var blueButton: WKInterfaceButton!
@IBAction func onRedButtonClick() {
if ypo {
openParentAppWithColor("Yellow")
}
else {
openParentAppWithColor("Red")
}
}
@IBOutlet weak var moreButton: WKInterfaceButton!
@IBAction func moreButtonClick() {
if !ypo {
ypo = true
redButton.setTitle("Yellow")
redButton.setColor(UIColor.yellowColor())
greenButton.setTitle("Purple")
greenButton.setColor(UIColor.purpleColor())
blueButton.setTitle("Orange")
blueButton.setColor(UIColor.orangeColor())
moreButton.setTitle("Back")
}
else {
ypo = false
redButton.setTitle("Red")
redButton.setColor(UIColor.redColor())
greenButton.setTitle("Green")
greenButton.setColor(UIColor.greenColor())
blueButton.setTitle("Blue")
blueButton.setColor(UIColor.blueColor())
moreButton.setTitle("More...")
}
}
@IBAction func onGreenButtonClick() {
if ypo {
openParentAppWithColor("Purple")
}
else {
openParentAppWithColor("Green")
}
}
@IBAction func onBlueButtonClick() {
if ypo {
openParentAppWithColor("Orange")
}
else {
openParentAppWithColor("Blue")
}
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
private func openParentAppWithColor(color: String) {
if ["color": color] != nil {
if !WKInterfaceController.openParentApplication(["color": color], reply: { (reply, error) -> Void in
println(reply)
}) {
println("ERROR")
}
}
}
}
我的问题是,比如说我点击了手表模拟器上的红色按钮。该操作将被调用,并在该操作中调用openParentApplicationWithColor("Red")
它应该调用WKInterfaceController.openParentApplication(["color": color], reply: { (reply, error) -> Void in })
的操作是在模拟器上打开父应用程序。它在后台打开它。因此,我手动打开它。当我手动打开应用程序时,背景是完全黑色的。
我怀疑问题是我没有在“功能”选项卡中启用“应用程序组”。为此,您需要加入开发人员计划。我应该加入它来做到这一点,还是还有其他问题?我正在使用 Xcode 6.2 Beta 3。提前谢谢大家!