2015-01-12 42 views
2

我正在开发一款在手表和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.提前谢谢大家!

回答

7

我已经测试过,可以确认openParentApplication:回复:是否不是要求启用应用组。

我明白,openParentApplication:回复,当被调用时,从Apple Watch打开iPhone应用程序。

此方法在后台打开iPhone应用。在之前的Xcode 6.2 beta版本中,应用程序确实在前台打开,但这不是预期的行为,也不是WatchKit Extension-iPhone应用程序通信在发布版本中的工作方式。

您仍然可以手动在前台启动iPhone应用程序,但这不是必需的,除非您要测试同时使用两者的用例。至少在目前可用的API中,iPhone应用程序无法由手表应用程序以编程方式启动到前台,并且手表应用程序无法以iPhone应用程序的编程方式启动。

一旦应用程序启动,您已经报告,当您希望它根据您发送的消息更改颜色时,屏幕仍为黑色。在iPhone应用程序中调用application:handleWatchKitExtension:reply:?如果您在WatchKit扩展程序中接收到执行的回复块,您应该知道它肯定会被调用,该扩展程序应该是从println(reply)输出某些内容。如果是这样,那么问题在于iPhone应用程序中的代码对您传递给它的消息做了些什么。在你的App应用程序的AppDelegate中看到你的方法实现会很有用。 (请注意,如果该方法未能调用回复块,它可能仍然在接收邮件,并且您不会收到回复,但是您会看到有关未收到回复的错误消息。)

请注意,在最初运行Watch扩展时,您将无法在Xcode中看到NSLog消息或获取断点,但您可以选择Debug> Attach to process> [在'可能目标'下选择您的iPhone应用程序],然后您将获取iPhone应用程序的日志和断点而不是Watch应用程序,同时仍然可以在手表模拟器中使用Watch应用程序。对调试最有用。

+0

谢谢你回答这个问题,但是当我手动打开它时,我得到一个黑色的屏幕,而不是我在观察窗口中单击的颜色。黑屏是否应该发生? – Tom

+0

你在iPhone应用程序中出现黑屏? –

+0

这是正确的。 – Tom

0

我正在使用Xcode 6.2 Beta 5.选择Debug> Attach to process> [在可能的目标下选择您的iPhone应用程序无法正常工作。我看到了NSLogs的Watch扩展,但没有在iPhone应用程序委托和iPhone应用程序的视图控制器。

1

不,您不需要使组使能ole父应用程序。您可以请求打开父IOS应用程序,并等待IOS应用程序的回复,而无需设置组应用程序。只需要在使用NSUserDefauts和套件名称在watchkit应用程序和IOS应用程序之间共享数据时设置组应用程序。

相关问题