Tom*_*Tom 2 ios swift ios-app-group watchkit
我正在开发一个在手表和iOS父应用程序之间进行通信的应用程序.它通过打开它将WatchKit扩展中的数据发送到父应用程序.我知道openParentApplication:reply,在调用时,会从Apple Watch打开iPhone应用程序.之后,application:handleWatchKitExtension:reply在应用程序的委托中调用.
从那里,您可以通过以下方式向视图控制器打开通知:
NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?)
Run Code Online (Sandbox Code Playgroud)
您可以在视图控制器中打开"aName":
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("handleWatchKitNotification:"),
name: "WatchKitSaysHello",
object: nil)
Run Code Online (Sandbox Code Playgroud)
这是我的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")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,例如我点击了手表模拟器上的红色按钮.该动作将被调用,并且在该动作中,它调用openParentApplicationWithColor("Red")哪个将调用WKInterfaceController.openParentApplication(["color": color], reply: { (reply, error) -> Void in })它应该做的是在模拟器上打开父应用程序.它在后台打开它.我因此手动打开它.当我手动打开应用程序时,背景是完全黑色的.
我怀疑问题是我没有在Capabilities选项卡中启用App Groups.为此,您需要加入开发人员计划.我应该加入它来做这个,还是有其他问题?我正在使用Xcode 6.2 Beta 3.提前谢谢大家!
我已经测试,并确认openParentApplication:回复:不不要求应用程序组启用.
我知道openParentApplication:reply,当被调用时,会从Apple Watch打开iPhone应用程序.
此方法在后台打开iPhone应用程序.在以前的Xcode 6.2测试版中,应用程序确实在前台开放,但这不是预期的行为,也不是WatchKit Extension-iPhone应用程序通信在发货版本中的工作方式.
您仍然可以手动在前台启动iPhone应用程序,但除非您要测试一次使用两者的用例,否则这不是必需的.至少使用当前可用的API,iPhone应用程序无法通过手表应用程序以编程方式启动到前台,并且iPhone应用程序无法以编程方式启动手表应用程序.
一旦应用程序启动,您已经报告屏幕仍然是黑色的,当您希望它根据您发送的消息更改颜色时.是application:handleWatchKitExtension:reply:在iPhone应用程序中调用?如果你收到回复块以便在你的WatchKit扩展程序中执行,你会知道它肯定被调用了,它应该从你的输出中输出一些东西println(reply).如果是这样,那么问题在于iPhone应用程序中的代码可以对您传递给它的消息执行某些操作.在iPhone应用程序的AppDelegate中查看该方法的实现会很有用.(注意,如果该方法无法调用回复块,它可能仍然在接收消息,您将无法获得回复,但随后您将看到有关未收到回复的错误消息.)
请注意,在最初运行Watch扩展时,您不会在Xcode中看到NSLog消息或获取断点,但您可以选择Debug> Attach to process> [在'Likely targets'下选择您的iPhone应用程序],然后您将获得日志和iPhone应用程序的断点而不是Watch应用程序,同时仍然可以在手表模拟器中使用Watch应用程序.最有用的调试.
| 归档时间: |
|
| 查看次数: |
2595 次 |
| 最近记录: |