gpt4 book ai didi

swift 2 : How to keep iOS and watchOS2 apps synchronized with WatchConnectivity?

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:37 26 4
gpt4 key购买 nike

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

InterfaceController.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) 这个if let message : String = countedLabel.text ?

有关更多信息,您可以查看此项目:Counting App (<- 项目的 URL)

最佳答案

我发现了问题,我无法对答案发表评论,因为评论太长了,所以我做了一个新的答案。问题出在 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)")
}
}

关于 swift 2 : How to keep iOS and watchOS2 apps synchronized with WatchConnectivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com