WCSessionDelegate
A delegate protocol that defines methods for receiving messages sent by a Wcsession object.
Declaration
protocol WCSessionDelegate : NSObjectProtocolOverview
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.
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:
sessionDidBecomeInactive(_:) (iOS only)
sessionDidDeactivate(_:) (iOS only)
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.
Topics
Managing Session Activation
Managing State Changes
sessionWatchStateDidChange(_:)sessionReachabilityDidChange(_:)sessionCompanionAppInstalledDidChange(_:)
Receiving Context Data
Receiving Immediate Messages
session(_:didReceiveMessage:)session(_:didReceiveMessage:replyHandler:)session(_:didReceiveMessageData:)session(_:didReceiveMessageData:replyHandler:)