絶対にやってはいけないこと
それはdelegate
を設定する前にactivateSession
を呼び出すこと。残念ながら例外とかエラーとか発生しないので呼び出す場所によってはアクティベートに失敗していることに気が付きません。
以下のコードはアクティベートに失敗します。
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.activateSession()
session.delegate = self
}
解決方法
ドキュメントに書いてある通りにWCSession
をアクティベートすること。
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
"IMPORTANT"としっかりと書いてあります。isSupported()
もちゃんと呼びなさいということです。
IMPORTANT
Always assign a delegate and activate your session before calling any session-related methods. The session must be configured and activated before sending messages or obtaining information about the state of the connection. Before activating the session, you may call the isSupported method to make sure that current device can use the Watch Connectivity framework.
まとめ
ちゃんとドキュメントに目を通しましょうってことなんですよね。。。