How to share data from iPhone to Apple Watch if watch app is in background

196 Views Asked by At

I need some help sending data from Watch to iPhone and the other way. If I send data from my watch to the iPhone and the iPhone app is not in multitasking the data is update once the iPhone app gets opened. But if I send data from my iPhone to the watch it only gets update on the watch if the watch app is visible. I can't get it to work if the app is in background. any suggestions? thank you very much!

InterfaceController

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController {

@IBOutlet weak var dataFromPhoneLabel: WKInterfaceLabel!

let session = WCSession.default

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    
    // Configure interface objects here.
    session.delegate = self
    session.activate()
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}
@IBAction func sendDataToPhoneButtonTapped() {
    let dataToPhone: [String: Any] = ["watch": "FromWatch" as Any]
    session.sendMessage(dataToPhone, replyHandler: nil, errorHandler: nil)
    }

}

extension InterfaceController: WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    //
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
    if let valueFromPhone = message["phone"] as? String {
        self.dataFromPhoneLabel.setText(valueFromPhone)
        }
    }

}

ViewController

import UIKit
import WatchConnectivity

class ViewController: UIViewController {

@IBOutlet weak var phoneToWatchTextField: UITextField!

var session: WCSession?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    if WCSession.isSupported() {
        session = WCSession.default
        session.delegate = self
        session.activate()
    }
}

@IBAction func sendToWatchButtonTapped(_ sender: Any) {
    if let validSession = self.session, validSession.isReachable {
        let dataToWatch: [String: Any] = ["phone": phoneToWatchTextField.text as Any]
        validSession.sendMessage(dataToWatch, replyHandler: nil, errorHandler: nil)
    }
}

}

extension ViewController: WCSessionDelegate {
func sessionDidBecomeInactive(_ session: WCSession) {
    //
}

func sessionDidDeactivate(_ session: WCSession) {
    //
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    //
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
DispatchQueue.main.async {
    if let valueFromWatch = message["watch"] as? String {
        phoneToWatchTextField.text = valueFromWatch
    }
}

}
0

There are 0 best solutions below