我在 iOS 和 WatchOS 上制作了一个计数应用程序,我希望应用程序能够同步。当我指望 WatchOS 时,iOS 标签上的数字必须与 WatchOS 上的相同,而当我指望 iOS 时,WatchOS 标签上的数字必须与一个 iOS 相同。这两个之一正在工作,当我指望 iOS 时,WatchOS 上的标签正在改变,这意味着它正在工作,但是当我指望 WatchOS 时,iOS 的标签并没有改变。
这是代码:
ViewController.swift
import UIKit
import WatchConnectivity
class ViewController: UIViewController, WCSessionDelegate {
var watchSession : WCSession?
var counter: Int {
return NSUserDefaults().integerForKey("counter")
}
@IBAction func resetButton(sender: AnyObject) {
NSUserDefaults().removeObjectForKey("counter")
countedLabel.text = "\(counter)"
if let message : String = countedLabel.text {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
}
@IBOutlet var countedLabel: UILabel!
@IBAction func countUpButton(sender: AnyObject) {
NSUserDefaults().setInteger(counter+1, forKey: "counter")
countedLabel.text = "\(counter)"
if let message : String = countedLabel.text {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
}
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
let message : String = applicationContext["message"] as! String
NSUserDefaults().setInteger(Int(message)!, forKey: "counted")
countedLabel.text = ("\(message)")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(WCSession.isSupported()){
watchSession = WCSession.defaultSession()
watchSession!.delegate = self
watchSession!.activateSession()
}
}
接口控制器.swift
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
var watchSession : WCSession?
var counted: Int {
return NSUserDefaults().integerForKey("counted")
}
@IBAction func resetButton() {
NSUserDefaults().removeObjectForKey("counted")
countedLabel.setText("\(counted)")
if let message : String = "\(counted)" {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
}
@IBOutlet var countedLabel: WKInterfaceLabel!
@IBAction func countUpButton() {
NSUserDefaults().setInteger(counted+1, forKey: "counted")
countedLabel.setText("\(counted)")
if let message : String = "\(counted)" {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
}
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
let message : String = applicationContext["message"] as! String
NSUserDefaults().setInteger(Int(message)!, forKey: "counted")
countedLabel.setText(message)
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if(WCSession.isSupported()){
watchSession = WCSession.defaultSession()
// Add self as a delegate of the session so we can handle messages
watchSession!.delegate = self
watchSession!.activateSession()
}
}
问题出InterfaceController.swift
在这部分代码中。
if let message : String = "\(counted)" {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
在ViewController.swift
(iOS)中工作时,那部分代码是:
if let message : String = countedLabel.text {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
那么,我可以在InterfaceController.swift (WatchOS) 上使用什么来代替if let message : String = "\(counted)" {
我在ViewController.swift (iOS) this上使用的 thisif let message : String = countedLabel.text
呢?
有关更多信息,您可以查看此项目:Counting App (<- URL to project)