How can I transfer an UIImage over WatchConnecitivity from the iPhone to the Apple Watch with no user interaction on the phone, and only loads because the watch calls for it programmatically. I need this because the image processing to create the UIImage uses logic unavailable in the Watchkit API, so it must be created from the phone. I have seem some examples of Watch Connectivity using:
func startSession() {
session?.delegate = self
session?.activateSession()
}
However, I am new to watch kit and am confused on how to use this session manager, particularly to go from the watch to the device instead of the other way around like I see in apple documentation. Can someone please provide an example of how to do this on both the watch and the phone to call for a UIImage on the phone from the watch?
To transfer files between your iPhone and your Apple Watch you should use Watch Connectivity, using WCSession to handle the communication properly. You can send an UIImage as NSData using the didReceiveMessageData delegate method of the WCSessionDelegate.
The first thing you should know is convert your UIImage to NSData and viceversa. You can use for this the following code:
If PNG images
let image = UIImage(named: "nameOfYourImage.jpg")
let data = UIImagePNGRepresentation(image)
If JPG images
let image = UIImage(named: "nameOfYourImage.jpg")
let data = UIImageJPEGRepresentation(image, 1.0)
Then you can use the WCSession to send the message like in the following way:
ViewController.swift
class ViewController: UIViewController, WCSessionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if WCSession.isSupported() {
WCSession.defaultSession().delegate = self
WCSession.defaultSession().activateSession()
}
let image = UIImage(named: "index.jpg")!
let data = UIImageJPEGRepresentation(image, 1.0)
WCSession.defaultSession().sendMessageData(data!, replyHandler: { (data) -> Void in
// handle the response from the device
}) { (error) -> Void in
print("error: \(error.localizedDescription)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
InterfaceController.swift
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
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() {
WCSession.defaultSession().delegate = self
WCSession.defaultSession().activateSession()
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func session(session: WCSession, didReceiveMessageData messageData: NSData, replyHandler: (NSData) -> Void) {
guard let image = UIImage(data: messageData) else {
return
}
// throw to the main queue to upate properly
dispatch_async(dispatch_get_main_queue()) { [weak self] in
// update your UI here
}
replyHandler(messageData)
}
}
In the above code when you open the ViewController it sends the UIImage, the above example is only for learning purposes, you have to handle it in a more proper way regarding the complexity of your project.
I hope this help you.
you have to transfer your image as NSData and then decode it on watch's side. You can have a look at my blog post where I covered similar case.
http://eluss.github.io/AppleWatch_iPhone_data_transfer/
This is not the way of doing it with session as I was not aware of it back then, but maybe it will help you get the whole logic.
In InterfaceController, if you use WCSessionDelegate, remember to extend method activationDidCompleteWith:
func session(_ session: WCSession, activationDidCompleteWith
activationState: WCSessionActivationState, error: Error?) {
}
Related
I am trying to add watch extension to my existing application. Login is compulsory for my application. How can i check from watch app if user logged-in or not... and as user gets loged-in, I want to pass that login-data from the application to the watchapplication. I don't know how to pass the login data to watch application.
Prior to WatchOS 2, you could share data between iOS companion app and watchOS app by using shared group container or iCloud to exchange data.
From WatchOS 2, since WatchKit extension now runs on Apple Watch itself, the extension must exchange data with the iOS app wirelessly. You will have to use WCSession class which is part of the WatchConnectivity framework. The framework is used to implement two-way communication between an iOS app and its paired watchOS app.
In iPhone companion app. Implement the following:
import WatchConnectivity
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
WCSession.default().activate()
}
}
#IBAction func loginAction(_ sender: Any) {
// In this case ApiHandler is just a class which performs REST call to authenticate login credentials.
ApiHandler.login(username: txtUsername.text!, password: txtPassword.text!, { (loginSuccess) in
if loginSuccess {
let dict = ["statusLogin": "success"]
WCSession.default().sendMessage(dict, replyHandler: { (replyMessage) in
print(replyMessage)
}) { (error) in
print(error.localizedDescription)
}
}
})
}
}
In watchOS app.
import WatchConnectivity
class BaseInterfaceController: WKInterfaceController {
override func willActivate() {
super.willActivate()
if WCSession.isSupported() {
WCSession.default().delegate = self
WCSession.default().activate()
}
}
}
extension BaseInterfaceController: WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: #escaping ([String : Any]) -> Swift.Void) {
if let loginStatus = message["statusLogin"] as? String {
if loginStatus == "success" {
// login has been success in iOS app
// perform further tasks, like saving in Userdefaults etc.
}
}
}
}
You can do this using Watch Connectivity. Specifically sending a boolean value with transferUserInfo(). This will allow you to send the data from the iPhone application when the user either logs in or out, and it will be delivered to the watch extension in the background.
Here is an example:
func updateUserLoggedIn(_ loggedInValue : Bool) {
if WCSession.isSupported() {
WCSession.default.transferUserInfo(["User Logged In" : loggedInValue])
}
}
Then you simply handle the transfer in your watch extension:
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
if let userInfoFromPhone = userInfo["User Logged In"] {
// Handle Result Accordingly
}
}
I would like to scan the NDEF data from my NFC-Tag (NXP NTAG213) using the Core NFC framework introduced in iOS 11.
I succeeded to read the Payload within the tag:
TNF=1, Payload Type=<54>, Payload ID=<>, Payload=<026a61e3 8193e382 93e381ab e381a1e3 81af0a>
I'd like to extract the payload part, so here's what I've tried:
print("payload.payload")
but error occurred.
Here's my source code:
import UIKit
import CoreNFC
class ViewController: UIViewController, NFCNDEFReaderSessionDelegate{
var payloadData = "HaveNoData"
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
print("エラーは発生しませんでした")
for message in messages{
for payload in message.records {
print (payload)
payloadData = String(describing: payload.payload)
}
}
print(payloadData)
}
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
print("Error: \(error.localizedDescription)")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
session.begin()
}
#IBAction func launch(_ sender: Any) {
let session:NFCNDEFReaderSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
session.begin()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
CoreNFC does not support parsing NFC NDEF message payload which is complex. I created an open source parser VYNFCKit to parse payload. Check my tutorial https://medium.com/#vinceyuan/reading-and-parsing-nfc-tag-on-ios-11-60f4bc7a11ea
I'm not sure if you can use describing: on payload.payload here. But I believe the old-fashion String from NSData would work. (Not a daily Swift coder so not sure how it handles this these days with Swift 3/4).
In Objective-C, I can do it with:
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
or in your case NSUTF8StringEncoding for the Japanese string.
Here's a sample project I made (in Objective-C).
I've tested on the Simulator and Device, somehow Watch Connectivity stops working after it's used once.
I'm passing data from Watch -> iPhone, and it only works once and then stops after that.
Any ideas?
iPhone ViewController:
var session: WCSession?
override func viewDidLoad() {
super.viewDidLoad()
if WCSession.isSupported() {
session = WCSession.default()
session?.delegate = self
session?.activate()
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
// Received Application Context from Watch
let type = applicationContext["watchType"]
print("Type iPhone: \(type)")
DispatchQueue.main.async {
self.type.text = "Type: \(type)"
}
}
Watch InterfaceController:
let session = WCSession.default()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
if WCSession.isSupported() {
session.delegate = self
session.activate()
}
}
#IBAction func startPressed() {
saveEverything()
}
func saveEverything() {
let watchContextType = ["watchType" : "Boxing/Running"]
do {
print("Type Watch: \(watchContextType)")
try session.updateApplicationContext(watchContextType)
} catch {
print("Didn't work")
}
}
When you use the updateApplicationContext(), you need to change the parameters for each call, otherwise the msg will not be delivered. I belive this is to conserve battery.
Anyway, try sending your message using sendMessage()or sendMessageData(), then the messages get delivered each time, even when they have the same contents. And, they are higher priority than updateApplicationContext so it's win-win :)
Here is the documentation: https://developer.apple.com/library/content/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1
If you want to Drain Battery of your apple watch app Quickly then above technique of #Jens is well and good.
Instead of shifting from updateApplicationContext() to sendMessage() better make small change in your project to get Desired result.
I will explain the question scenario and then solution along with Demo app :-
#IBAction func sliderChange(_ value: Float)
{
let str:String?
switch value {
case 0...7 :
str = "Running"
case 8...14 :
str = "Sleeping"
default:
str = "normal"
}
updateContext(value: str!)
}
func updateContext(value:String) {
let dictionary = [ "watchType" : value ]
do {
print("update application context is called do statemet")
try session?.updateApplicationContext(dictionary)
}
catch{
print("this is the catch statement")
}
}
With update in slider Values in Watch ,iPhone values get updated .As you can see there is repetition of values for iPhone i.e
When sliderValue are from 0 to 7 values remain "Running" && "Sleeping" for
8 to 14 .
App work fine if I varies values of slide and desired result is reflected in iPhone in normal scenario.
Scenrio where it fails :-
i)Change the slider values from 0 to 3 then "Running" is reflected in iPhone. Thant's fine .
ii)Now close the iPhone application then change the slider values from 3 to 5 no we can see real problem when iPhone is opened back.
iii)Values is not triggered to iPhone .
Due to internal caching mechanism of updateApplicationContext() restrict
to trigger of duplicate values to iPhone .
Store the last updated state in didReceiveApplicationContext() and display state according to stored value .
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
checkStatus()
updateSavedState()
}
func checkStatus() {
print("check status")
if WCSession.isSupported() {
session = WCSession.default()
session?.delegate = self
session?.activate()
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
let type = applicationContext["watchType"]!
DispatchQueue.main.async {
self.updateLabel.text = " Type: \(type)"
UserDefaults.standard.set(type, forKey: "savedState") //setObject
}
}
func updateSavedState() {
self.updateLabel.text = UserDefaults.standard.string(forKey: "savedState")
}
Now everything is work perfect.
Demo App.
I updated my app to the latest swift 2.0 syntax. In doing so, My watchkit app has become broken. The issue is the watchkit app references a class that references the framework AVFoundation. WatchOS2 apparently now no longer supports some of the standard frameworks:
Support for network-based operations includes the following technologies:
WatchKit extensions can access the network directly through an
NSURLSession object. WatchKit extensions have full access to the
NSURLSession capabilities, including the ability to download files in
the background. For information on how to use this class, see URL
Loading System Programming Guide. The Watch Connectivity framework
supports bidirectional communication between your Watch app and iOS
app. Use this framework to coordinate activities between the two apps.
See Communicating with Your Companion iOS App.
Available System Technologies for WatchKit
So now I cannot compile the watch kit code as "no such module found" is an error message when trying to use the AVFoundation framework. How can I get around this and keep referencing that class and framework in my apple watch app. Should I be communicating data between the phone and the watch? Is there a way to link the framework to the extension?
What I am trying to do is the following, in my InterfaceController:
override func willActivate() {
super.willActivate()
let defaultsShared = NSUserDefaults(suiteName: "somesharedappgroup")
let defaults = NSUserDefaults.standardUserDefaults()
if let barcodeString = defaultsShared!.objectForKey("barcode") as? String {
if let barcodeContent = RSUnifiedCodeGenerator.shared.generateCode(barcodeString, machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code) {
barcode.setImage(barcodeContent)
label.setText("ID: \(barcodeString)")
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
} else {
label.setText("Please setup extensions in the settings of SHPID.")
barcode.setImage(nil)
}
}
The RSUnifiedCodeGenerator being a class that utilizes AVFoundation to generate barcode images from strings. Furthermore, the type that generator takes is an AVObject: AVMetadataObjectTypeCode39Code. This solution worked well in the first WatchOS, but now remains broken in OS 2. I see that WatchConnectivity may be a solution, and have it just pass me the barcode from the phone itself, but that would require I stop supporting iOS 8. What is the best solution, if any, for using AVFoundation with WatchOS 2. If I can not do that, how else should I go about passing this image to the watch from the phone when called. Thanks.
This is an example on how you could use WatchConnectivity for your app.
Please not that this example is rough and does not handle error. The session management should also get some attention for a stable product.
iPhone AppDelegate
import UIKit
import WatchConnectivity
import AVFoundation
import RSBarcodes
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
return true
}
// On Watch sends the message.
// Will not reply as we will push a data message with image.
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
if "generateBarcode" == message["id"] as! String {
let code = message["code"] as! String
let barcodeImage = RSUnifiedCodeGenerator.shared.generateCode(code,
machineReadableCodeObjectType: AVMetadataObjectTypeCode39Code)!
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
session.sendMessageData(UIImagePNGRepresentation(barcodeImage)!,
replyHandler: nil, errorHandler: nil)
}
}
}
}
Watch InterfaceController
import WatchKit
import Foundation
import WatchConnectivity
class InterfaceController: WKInterfaceController, WCSessionDelegate {
#IBOutlet var barcodeImage: WKInterfaceImage!
override func willActivate() {
super.willActivate()
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
// Send a message requesting a barcode image
session.sendMessage(
["id": "generateBarcode", "code": "2166529V"],
replyHandler: nil, // Do not handle response, iPhone will push a data message
errorHandler: nil)
}
}
// On iPhone pushes a data message
func session(session: WCSession, didReceiveMessageData messageData: NSData) {
barcodeImage.setImage(UIImage(data: messageData))
}
}
I think that using WatchConnectivity is the right thing to do.
For previous version support if the only dealer breaker is AVMetadataObjectTypeCode39Code, maybe you can print its value and pass it to the function directly?
I am trying to pass data from my app into my Apple Watch app. Basically, I am using the same method as I used for creating the today widget and so I am passing data through NSUserDefaults.
The problem is, that when I run my app, the data does not update the labels in the Watch app as I would expect it to.
Here is what I have...
override init(context: AnyObject?) {
// Initialize variables here.
super.init(context: context)
// Configure interface objects here.
NSLog("%# init", self)
var defaults = NSUserDefaults(suiteName: "group.AffordIt")
var totalBudgetCalculation = ""
if (defaults!.stringForKey("totalBudgetWidget") != nil) {
println("Worked")
totalBudgetCalculation = defaults!.stringForKey("totalBudgetWidget")!
initialBudgetLabel.setText("Initial: \(totalBudgetCalculation)")
}
var currentBudgetCalculation = ""
if (defaults!.stringForKey("currentBudgetWidget") != nil) {
currentBudgetCalculation = defaults!.stringForKey("currentBudgetWidget")!
currentBudgetLabel.setText("Current: \(currentBudgetCalculation)")
}
}
I tried putting this code in willActivate(), however that doesn't seem to make a difference.
Anyone know where I am going wrong?
This applies to OS 1 only. See below for better answers.
I got it working using your method. I guess there's a couple of things you can check:
1) Are you synchronising the defaults after you set the value:
defaults?.synchronize();
NSLog("%# ", defaults?.dictionaryRepresentation())
2) Have you enabled the App Group in both your app and your extension?
3) Are you using the correctly named app group when constructing the NSDefaults? For example, I use:
NSUserDefaults(suiteName: "group.com.brindysoft.MyWatch");
Once all that's set up I run the app, set the value in the defaults, then run the glance target which reads the value from the default and that seems to work!
Still stuck? check your app groups in your apple account
The accepted answer applies to apple watch os 1. See NSUserDefaults not working on Xcode beta with Watch OS2
For OS2 - you will need to use the WatchConnectivity frameworks and implement the WCSessionDelegate.
import WatchConnectivity
import WatchKit
#available(iOS 9.0, *)
var alertDelegate:HomeIC? = nil
public class WatchData: NSObject,WCSessionDelegate {
var session = WCSession.defaultSession()
//
class var shared: WatchData {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: WatchData? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = WatchData()
}
return Static.instance!
}
public func session(session: WCSession, didReceiveFile file: WCSessionFile){
print(__FUNCTION__)
print(session)
}
public func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
print(__FUNCTION__)
print(session)
alertDelegate?.showMessage("didReceiveApplicationContext")
}
public func sessionReachabilityDidChange(session: WCSession){
print(__FUNCTION__)
print(session)
print("reachability changed:\(session.reachable)")
let text = session.reachable ? "reachable" : "unreachable"
alertDelegate?.showMessage(text)
}
public func sessionWatchStateDidChange(session: WCSession) {
print(__FUNCTION__)
print(session)
print("reachable:\(session.reachable)")
// alertDelegate?.showMessage("sessionWatchStateDidChange")
if !session.receivedApplicationContext.keys.isEmpty {
alertDelegate?.showMessage(session.receivedApplicationContext.description)
}
}
public func session(session: WCSession, didReceiveMessageData messageData: NSData){
if !session.receivedApplicationContext.keys.isEmpty {
alertDelegate?.showMessage(session.receivedApplicationContext.description)
}
}
public func session(session: WCSession, didReceiveMessage message: [String : AnyObject]){
print(__FUNCTION__)
if let data = message["data"] {
alertDelegate?.showMessage(data as! String)
return
}
}
public func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print(__FUNCTION__)
if let data = message["data"] {
alertDelegate?.showMessage(data as! String)
return
}
guard message["request"] as? String == "showAlert" else {return}
}
public func activate(){
if WCSession.isSupported() { // it is supported
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
print("watch activating WCSession")
} else {
print("watch does not support WCSession")
}
if(!session.reachable){
print("not reachable")
return
}else{
print("watch is reachable")
}
}
}
Sample Usage
class HomeIC: WKInterfaceController {
// MARK: Properties
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Initialize the `WCSession`.
WatchData.shared.activate()
alertDelegate = self
}
internal func showMessage(msg:String){
let defaultAction = WKAlertAction(title: msg, style: WKAlertActionStyle.Default) { () -> Void in }
let actions = [defaultAction]
self.presentAlertControllerWithTitle( "Info", message: "", preferredStyle: WKAlertControllerStyle.Alert, actions: actions)
}
}
in my iphone code / I can invoke sharing data here
if #available(iOS 9.0, *) {
WatchData.shared.sendInbox()
} else {
// Fallback on earlier versions
}
And somewhere else I have another discrete singleton for watch data session.
#available(iOS 9.0, *)
public class WatchData: NSObject,WCSessionDelegate {
var session = WCSession.defaultSession()
var payload:String = ""
class var shared: WatchData {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: WatchData? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = WatchData()
}
return Static.instance!
}
public func sessionReachabilityDidChange(session: WCSession){
print(__FUNCTION__)
print(session)
print("reachability changed:\(session.reachable)")
if (session.reachable){
}
}
public func sessionWatchStateDidChange(session: WCSession) {
print(__FUNCTION__)
print(session)
print("reachable:\(session.reachable)")
}
public func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print(__FUNCTION__)
guard message["request"] as? String == "showAlert" else {return}
guard let m = message["m"] as? String else { return }
print("msg:",m)
}
public func sendInbox(){
if (!session.reachable){
if WCSession.isSupported() { // it is supported
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
print("iphone activating WCSession")
} else {
print("iphone does not support WCSession")
}
session.activateSession()
}
if(session.paired){
if(session.watchAppInstalled){
print("paired | watchAppInstalled")
}
}else{
print("not paired | or no watchAppInstalled")
}
if(!session.reachable){
print("not reachable")
return
}else{
/*let transfer:WCSessionUserInfoTransfer = (session.transferUserInfo(["data" : "Test2"]) as WCSessionUserInfoTransfer?)!
if(transfer.transferring){
print("-> iphone")
}else{
print("!-> iphone")
}*/
session.sendMessage(["data" :"test"],
replyHandler: { reply in
},
errorHandler: { error in
print(error)
})
}
}
}
Refer to sample watch os2 app
https://github.com/shu223/watchOS-2-Sampler/tree/20eeebeed66764d0814603e97d3aca5933236299
As #johndpope said, shared NSUserDefaults no longer work on WatchOS2.
I'm posting a simplified solution that's not as full featured as john's but will get the job done in most cases.
In your iPhone App, follow these steps:
Pick find the view controller that you want to push data to the Apple Watch from and add the framework at the top.
import WatchConnectivity
Now, establish a WatchConnectivity session with the watch and send some data.
if WCSession.isSupported() { //makes sure it's not an iPad or iPod
let watchSession = WCSession.defaultSession()
watchSession.delegate = self
watchSession.activateSession()
if watchSession.paired && watchSession.watchAppInstalled {
do {
try watchSession.updateApplicationContext(["foo": "bar"])
} catch let error as NSError {
print(error.description)
}
}
}
Please note, this will NOT work if you skip setting the delegate, so even if you never use it you must set it and add this extension:
extension MyViewController: WCSessionDelegate {
}
Now, in your watch app (this exact code works for Glances and other watch kit app types as well) you add the framework:
import WatchConnectivity
Then you set up the connectivity session:
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
let watchSession = WCSession.defaultSession()
watchSession.delegate = self
watchSession.activateSession()
}
and you simply listen and handle the messages from the iOS app:
extension InterfaceController: WCSessionDelegate {
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
print("\(applicationContext)")
dispatch_async(dispatch_get_main_queue(), {
//update UI here
})
}
}
That's all there is to it.
Items of note:
You can send a new applicationContext as often as you like and it
doesn't matter if the watch is nearby and connected or if the watch
app is running. This delivers the data in the background in an
intelligent way and that data is sitting there waiting when the
watch app is launched.
If your watch app is actually active and running, it should receive
the message immediately in most cases.
You can reverse this code to have the watch send messages to the
iPhone app the same way.
applicationContext that your watch app receives when it is viewed will ONLY be the last message you sent. If you sent 20 messages before the watch app is viewed, it will ignore the first 19 and handle the 20th one.
For doing a direct/hard connection between the 2 apps or for background file transfers or queued messaging, check out the WWDC video.
Another way to communicate between the app and the watch is via wormhole:
https://github.com/mutualmobile/MMWormhole
Send:
[self.wormhole passMessageObject:#{#"titleString" : title}
identifier:#"messageIdentifier"];
id messageObject = [self.wormhole messageWithIdentifier:#"messageIdentifier"];
Recieve:
[self.wormhole listenForMessageWithIdentifier:#"messageIdentifier"
listener:^(id messageObject) {
// Do Something
}];
Just use watch connectivity for communicate between these two platform you can read more about this in apple document
https://developer.apple.com/documentation/watchconnectivity