openParentApplication:reply是否需要启用应用程序组功能?

时间:2015-01-12 00:53:25

标签: ios swift watchkit ios-app-group

我正在开发一款在手表和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 })这应该做的是在模拟器上打开父应用程序。它会在背景中打开它。我因此手动打开它。当我手动打开应用程序时,背景是完全黑色的。
我怀疑问题是我没有在Capabilities选项卡中启用App Groups。为此,您需要加入开发人员计划。我应该加入它来做这个,还是有其他问题?我正在使用Xcode 6.2 Beta 3.提前谢谢大家!

3 个答案:

答案 0 :(得分:7)

我已经过测试,可以确认openParentApplication:reply:要求启用应用组。

  

我了解openParentApplication:reply,在调用时,会从Apple Watch打开iPhone应用程序。

此方法在后台打开iPhone应用 。在以前的Xcode 6.2测试版中,应用程序确实在前台开放,但这不是预期的行为,而且不是WatchKit Extension-iPhone应用程序通信在发货版本中的工作方式。

您仍然可以手动在前台启动iPhone应用程序,但除非您想要测试一次使用两者的用例,否则这不是必需的。至少使用当前可用的API,iPhone应用程序无法通过手表应用程序以编程方式启动到前台,并且手表应用程序无法通过iPhone应用程序以编程方式启动。

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我正在使用Xcode 6.2 Beta 5.选择Debug>附加到流程> [在可能的目标下选择您的iPhone应用程序无效。我看到了NSLogs for Watch扩展,但没有看到iPhone应用程序委托和iPhone应用程序的控制器。

相关问题