2

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

4

3 回答 3

7

我已经测试并且可以确认 openParentApplication:reply: 不需要启用应用程序组。

我了解 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 > [在“可能的目标”下选择您的 iPhone 应用程序],然后您将获得日志和iPhone 应用程序而不是 Watch 应用程序的断点,同时仍然能够在手表模拟器中使用 Watch 应用程序。对调试最有用。

于 2015-01-12T01:49:08.180 回答
1

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

于 2015-02-06T17:47:26.843 回答
0

我正在使用 Xcode 6.2 Beta 5。选择 Debug > Attach to process > [在可能的目标下选择您的 iPhone 应用程序不起作用。我在 iPhone 应用程序委托和 iPhone 应用程序的视图控制器中看到了 Watch 扩展的 NSLogs。

于 2015-02-06T14:26:32.557 回答