Sunday, May 29, 2016

Watch Connectivity Framework

The WCSession class facilitates communication between a WatchKit extension and its companion iOS app. Both processes must create and configure an instance of this class at some point during their execution. When both session objects are active, the two processes can communicate immediately by sending messages back and forth. When only one session is active, the active session may still send updates and transfer files, but those transfers happen opportunistically in the background.


if ([WCSession isSupported]) {
    WCSession* session = [WCSession defaultSession];
    session.delegate = self;
    [session activateSession];
}

You may initiate data transfers to a counterpart app only when the activationState property is set to WCSessionActivationStateActivated. 

Below are the communication options between watch app and companion app

Use the updateApplicationContext:error: method to communicate recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state. For example, an iOS app that supports Background App Refresh can use part of its background execution time to update the corresponding Watch app. This method overwrites the previous data dictionary, so use this method when your app needs only the most recent data values.

Use the sendMessage:replyHandler:errorHandler: or sendMessageData:replyHandler:errorHandler: method to transfer data to a reachable counterpart. These methods are intended for immediate communication between your iOS app and WatchKit extension. The reachable property must currently be YES for these methods to succeed.

Use the transferUserInfo: method to transfer a dictionary of data in the background. The dictionaries you send are queued for delivery to the counterpart and transfers continue when the current app is suspended or terminated.

Use the transferFile:metadata: method to transfer files in the background. Use this method in cases where you want to send more than a dictionary of values. For example, use this method to send images or file-based documents.


references:
https://developer.apple.com/library/watchos/documentation/WatchConnectivity/Reference/WCSession_class/

No comments:

Post a Comment