From WatchOS 1, there are only limited ways to communicate between Apple Watch and iPhone. In WatchOS2 , Watch Connectivity framework enables third-party developers to have better options to send and receive data from Watch. Additional safe guards are provided to check if the connectivity is available and well-established.
//check if the device supports Watch Connectivity (iPhone supports but iPad does not)
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
sesssion.activateSession()
}
//WCSessionDelegate methods
//called whenever the session had changed
func sessionWatchStateDidChange(_ session: WCSession) {
//use this to check if the watch and iPhone are paired
if ( session.paired ) {
...
}
//check if the watch has installed the watch app
if ( session.watchAppInstalled ) {
...
}
//watchDirectoryURL is not nil if the watch has installed the watch app
if let session.watchDirectoryURL {
}
//check if the complication is enabled on watch face
if (session.complicationEnabled ) {
}
}
Types
//sending
do {
let context = // Create context dictionary with latest state
try WCSession.defaultSession().updateApplicationContext(context)
} catch {
// Handle any errors
}
//receiving
func session(session: WCSession, didReceiveApplicationContext:
applicationContext: [String : AnyObject]) {
// Handle application context dictionary
}
//Sending
let userInfo = // Create dictionary of userInfo
//get transfer object for cancelling
let userInfoTransfer = WCSession.defaultSession().transferUserInfo(userInfo)
//Check outstanding items for transferring
let transfers = WCSession.defaultSession().outstandingUserInfoTransfers()
//Reciving
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
// Handle incoming user info dictionary
}
//Sending
let url = // Retrieve URL of file
let metadata = // Create dictionary of metadata
let fileTransfer = WCSession.defaultSession().transferFile(url,
metadata:metadata)
//Outstanding queue
let transfers = WCSession.defaultSession().outstandingFileTransfers()
// Receiver Callback
func session(session: WCSession, didReceiveFile file: WCSessionFile) {
// Handle file URL and metadata in WCSessionFile object
}
Live communication
session.reachable
to check if the interactive messaging is availablefunc sendMesage(message:, replyHandler:, errorHandler:)
func sendMessageData(data:, replyHandler:, errorHandler:)
Optional handler used for confirmation by receiver
func session(session: WCSession, didReceiveMessage message: [String :
AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
// Handle message, return reply
}
func session(session: WCSession, didReceiveMessage message: [String :
AnyObject]) {
// Handle message
}
didReceiveIncomingPushWithPayload
to transfer timeLineEntry
to the watchsession.transferCurrentComplicationUserInfo(presentTimeLineEntry)
to transferfunc session(session: WCSession, didReceiveUserInfo userInfo: [String :
AnyObject])
to handle the receiver and update complication