Search code examples
iosswiftreachabilitywatchos-2

watchOS 2 Reachability in Swift


I have looked around at some of the similar answers but none of these seem to be helping me.

I seem to be having a problem with my application. I have created the single view application and also added in the WCSessionDelegate into my extension.

import WatchKit

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate { }

I have also checked the session if the session is not there and told it to print out if it cant find it

guard WCSession.isSupported() else {
  print("Session is not supported")
  return
}

let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()

However I am still getting a No WatchConnectivity error. I'm confused as If the session is working then I think I have it coded right?


Solution

  • Answer

    Whilst waiting for your code, I think I can see the issue. Where you have declared the session delegate as a delegate here:

    import WatchKit
    
    class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate { }
    

    You need to also import WatchConnectivity

    like so:

    import WatchKit
    import WatchConnectivity
    
    class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate { }
    

    Let me know if his helps!

    Tutorial

    There is a tutorial which explains exactly this issue: http://ios-blog.co.uk/tutorials/swift/watchos-2-checking-reachability/

    Plugin

    Also in the same search is a nift plugin you can use: https://github.com/ashleymills/Reachability.swift - This would be my option if you're not confident enough

    Tip

    (Google is your friend)