我正在开发一个在手表和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 …Run Code Online (Sandbox Code Playgroud)