Protocol

WCSessionDelegate

The WCSessionDelegate protocol defines methods for responding to messages sent by a WCSession object. Session objects are used to communicate between a WatchKit extension and the companion iOS app on a paired and active iPhone. When configuring your session object, you must specify a delegate object that implements this protocol. The session calls your delegate methods to deliver incoming data from the counterpart app and to manage session-related changes.

Overview

Most methods of this protocol are optional. You implement the methods you need to respond to the data transfer operations that your apps support. However, apps must implement the session(_:activationDidCompleteWith:error:) method, supporting asynchronous activation. On iOS, you must also implement the sessionDidBecomeInactive(_:) and sessionDidDeactivate(_:) methods, supporting multiple Apple Watches.

The WCSession object calls the methods of its delegate serially, so your method implementations do not need to be reentrant. Immediate messages can be sent only while both the WatchKit extension and iOS app are running. By contrast, context updates and file transfers can be initiated at any time and delivered in the background to the other device. All transfers are delivered in the order in which they were sent.

Supporting Communication with Multiple Apple Watches

An iPhone running iOS 9.3 or later may pair with more than one Apple Watch running watchOS 2.2 or later. Implement the following methods in your session delegate:

Use the activation-related methods to track the activation state of the session in your iOS app. With Auto Switch enabled on the user’s iPhone, the session automatically moves to the inactive state when the user puts on a different Apple Watch than the one that is currently active. (If Auto Switch is disabled, the user must manually select which watch is active.) While your iOS app is in the inactive state, the system finishes delivering any data that has been received before moving your app to the deactivated state. While inactive or deactivated, you cannot initiate any new transfers. When your iOS app reaches the deactivated state, call the session’s activate() method again to connect to the new Apple Watch.

For more information about the flow of messages when a user switches from one Apple Watch to another, see WCSession.

Symbols

Managing Session Activation

func sessionDidBecomeInactive(WCSession)

Called when the session prepares to stop communicating with the current Apple Watch.

func sessionDidDeactivate(WCSession)

Called after all data from the previous session has been delivered and communication with the Apple Watch has ended.

Managing State Changes

func sessionWatchStateDidChange(WCSession)

Called when a feature is enabled or disabled.

func sessionReachabilityDidChange(WCSession)

Called when the reachability of the counterpart session changes.

Receiving Context Data

func session(WCSession, didReceiveApplicationContext: [String : Any])

Called when the session receives context data from the counterpart.

Receiving Immediate Messages

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

Called when an immediate message arrives.

func session(WCSession, didReceiveMessageData: Data)

Called when an immediate data message arrives.

func session(WCSession, didReceiveMessageData: Data, replyHandler: (Data) -> Void)

Called when an immediate data message arrives and requires a response.

Managing Data Dictionary Transfers

func session(WCSession, didReceiveUserInfo: [String : Any] = [:])

Called when a data dictionary is received successfully.

func session(WCSession, didFinish: WCSessionUserInfoTransfer, error: Error?)

Called when a data transfer operation finished successfully or because of an error.

Managing File Transfers

func session(WCSession, didReceive: WCSessionFile)

Called when a file is received successfully.

func session(WCSession, didFinish: WCSessionFileTransfer, error: Error?)

Called when a file transfer finished successfully or because of an error.

Relationships

Inherits From