LoginSignup
5
5

More than 5 years have passed since last update.

WCSessionで絶対にやってはいけないこと

Posted at

絶対にやってはいけないこと

それは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.

まとめ

ちゃんとドキュメントに目を通しましょうってことなんですよね。。。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5