我正在开发一款在手表和iOS父应用程序之间进行通信的应用程序。它通过打开WatchKit扩展将数据发送到父应用程序。我知道openParentApplication:reply
被叫时会从Apple Watch打开iPhone应用程序。之后,application:handleWatchKitExtension:reply
在应用的代理中被调用。openParentApplication: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 })
这应该做的是在模拟器上打开父应用程序。它将在背景中打开它。我,因此手动打开它。当我手动打开应用程序时,背景是完全黑色的。
我怀疑问题是我没有在Capabilities选项卡中启用App Groups。为了做到这一点,您需要参与开发人员计划。我应该加入它来做到这一点,还是存在另一个问题?我正在使用Xcode 6.2 Beta 3.提前谢谢大家!
谢谢你回答这个问题,但是当我手动打开它时,我得到一个黑色的屏幕,而不是我在观察窗口中单击的颜色。黑屏是否应该发生? – Tom
你在iPhone应用程序中出现黑屏? –
这是正确的。 – Tom