Swift 2:如何保持iOS和watchOS2应用与WatchConnectivity同步?
Swift 2: How to keep iOS and watchOS2 apps synchronized with WatchConnectivity?
我在 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) 上使用什么来代替我在 [=45 上使用的这个 if let message : String = "\(counted)" {
=] (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)")
}
}
我在 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) 上使用什么来代替我在 [=45 上使用的这个 if let message : String = "\(counted)" {
=] (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)")
}
}