Reputation:
I made a Counting App on iOS and WatchOS, and I want apps to be synchronized. When I count on WatchOS the number on label of iOS have to be same as on WatchOS and when I count on iOS the number on label of WatchOS have to be same as one iOS. One of these two is workin, when I count on iOS the label on WatchOS is changing, that means that is working, but when I count on WatchOS the label of iOS is not changing.
Here is code:
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()
}
}
The problem is in InterfaceController.swift
in this part of code.
if let message : String = "\(counted)" {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
While in ViewController.swift
(iOS) is working and that part of code is:
if let message : String = countedLabel.text {
do {
try watchSession?.updateApplicationContext(
["message" : message]
)
} catch let error as NSError {
NSLog("Updating the context failed: " + error.localizedDescription)
}
}
So, what can I use on InterfaceController.swift (WatchOS) instead of this if let message : String = "\(counted)" {
as I used on ViewController.swift (iOS) this if let message : String = countedLabel.text
?
For more you can check this project: Counting App (<- URL to project)
Upvotes: 3
Views: 263
Reputation:
I found problem, I couldn't comment on answer because comment is to long so I made a new answer. The problem is in ViewController.swift
instead of using
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
let message : String = applicationContext["message"] as! String
NSUserDefaults().setInteger(Int(message)!, forKey: "counted")
countedLabel.text = ("\(message)") }
it's supposed to use
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)")
}
}
Upvotes: 2