3

我在 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)

4

1 回答 1

2

我发现了问题,我无法对答案发表评论,因为评论太长了,所以我做了一个新的答案。问题在于ViewController.swift而不是使用

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
let message : String = applicationContext["message"] as! String
NSUserDefaults().setInteger(Int(message)!, forKey: "counted")
countedLabel.text = ("\(message)") }

它应该使用

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
    dispatch_async(dispatch_get_main_queue()) { [unowned self] in
        let message : String = applicationContext["message"] as! String
        NSUserDefaults().setInteger(Int(message)!, forKey: "counter")
        self.countedLabel.text = ("\(message)")
    }
}
于 2015-12-27T23:13:18.863 回答