0

Have an application that needs to share a file and then a user info dictionary when the watch app becomes active but regardless of whether the iOS app is active. What is the best way to trigger that request from the iPhone to the Watch?

Grant Isom
  • 150
  • 11

1 Answers1

-3

UserDefaults only in WatchOS 1 not in latest WatchOS .

You can share your "userinfo" by enabling the Capabilites of group on both application and watch target and sharing by the Userdefaults among the Targets(iPhone and Watch).

//iPhone sharing Userinfo 
func sharedUserInfo() {

        if let userDefaults =  UserDefaults(suiteName: "group.watch.app.com" ) {
            userDefaults.set( userinfo as AnyObject, forKey: "UserInfo")
            userDefaults.synchronize()
            }

        }


//Watch extracting the info 
 func sharedInfo() {
        if let userDefaults = UserDefaults(suiteName: "group.watch.app.com") {
            let userInfo = userDefaults.string(forKey: "UserInfo")
           }

       }

For Watch connectivity we can implement simply by :-

 //  Watch Side
 //  InterfaceController.swift
 //  WatchKit Extension

import WatchKit
import Foundation
import WatchConnectivity

class InterfaceController: WKInterfaceController,WCSessionDelegate {

@IBOutlet var textLabel: WKInterfaceLabel!

var session:WCSession?

override func awake(withContext context: Any?) {
    super.awake(withContext: 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()
    checkSupportOfSession()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

func checkSupportOfSession() {
    if( WCSession.isSupported() ) {
        self.session = WCSession.default()
        self.session?.delegate = self
        self.session?.activate()
    }
}

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

func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {

    let message:String = message["textIndex"] as! String
    textLabel.setText(message)
    print(message)

}

}

//Application side Code

import UIKit
import WatchConnectivity

class ViewController: UIViewController,WCSessionDelegate {

@IBOutlet weak var textWord: UITextField!
var session:WCSession?

override func viewDidLoad() {
    super.viewDidLoad()
    checkSupportOfSession()
}

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

func checkSupportOfSession() {
    if( WCSession.isSupported() ) {
        self.session = WCSession.default()
        self.session?.delegate = self
        self.session?.activate()
    }
}



@available(iOS 9.3, *)
public func sessionDidBecomeInactive(_ session: WCSession)
{
    print("sessionS 2")
}

@available(iOS 9.3, *)
public func sessionDidDeactivate(_ session: WCSession){

}

@IBAction func sendTextToWatch(_ sender: Any) {
    print("send text to watch amount")
     if let textName = textWord.text {
        session?.sendMessage(["textIndex" : textName as String], replyHandler: nil, errorHandler: nil)
    }

}

}

https://github.com/shrawan2015/Application-Demo-StackOverFlow/tree/master/WatchOS

Shrawan
  • 7,128
  • 4
  • 29
  • 40
  • On WatchOS 1 you could, but not 2 or 3. – Grant Isom Nov 23 '16 at 19:43
  • According to below document there are no such constraints . https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html – Shrawan Nov 23 '16 at 19:49
  • My bad we cannot use in watch OS 2 . – Shrawan Nov 24 '16 at 06:44
  • @grant Apple have not mention "limitation of Userdefaults" in there document of sharing info in the watch-OS section . I was sure that we can use userdefults but after research I was found WatchConnectivity framework leads to transfer data . – Shrawan Nov 25 '16 at 06:54