我正在開發一款在手錶和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