Skip to content

Instantly share code, notes, and snippets.

@johndpope
Last active August 29, 2015 14:28
Show Gist options
  • Save johndpope/d07e7df2986461276a9d to your computer and use it in GitHub Desktop.
Save johndpope/d07e7df2986461276a9d to your computer and use it in GitHub Desktop.
import WatchConnectivity
import WatchKit
@available(iOS 9.0, *)
var alertDelegate:HomeIC? = nil
public class WatchData: NSObject,WCSessionDelegate {
var session = WCSession.defaultSession()
//
class var shared: WatchData {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: WatchData? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = WatchData()
}
return Static.instance!
}
public func sessionReachabilityDidChange(session: WCSession){
print(__FUNCTION__)
print(session)
print("reachability changed:\(session.reachable)")
alertDelegate?.showMessage("reachability changed")
}
public func sessionWatchStateDidChange(session: WCSession) {
print(__FUNCTION__)
print(session)
print("reachable:\(session.reachable)")
alertDelegate?.showMessage("sessionWatchStateDidChange")
}
public func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
print(__FUNCTION__)
guard message["request"] as? String == "showAlert" else {return}
alertDelegate?.showMessage(message["request"] as! String)
}
public func activate(){
if WCSession.isSupported() { // it is supported
session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
print("watch activating WCSession")
} else {
print("watch does not support WCSession")
}
if(!session.reachable){
print("not reachable")
return
}else{
print("watch is reachable")
}
}
}
// Sample Usage
class HomeIC: WKInterfaceController {
// MARK: Properties
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Initialize the `WCSession`.
WatchData.shared.activate()
alertDelegate = self
}
internal func showMessage(msg:String){
let defaultAction = WKAlertAction(title: msg, style: WKAlertActionStyle.Default) { () -> Void in }
let actions = [defaultAction]
self.presentAlertControllerWithTitle( "Message Received", message: "", preferredStyle: WKAlertControllerStyle.Alert, actions: actions)
}
}
@johndpope
Copy link
Author

// this is used targeting to my iphone - targeting
in my view controller I'm sending data by invoking this

 if #available(iOS 9.0, *) {
        WatchData.shared.sendInbox()
    } else {
        // Fallback on earlier versions
    }

And somewhere else on iphone - I have another discrete singleton for watch data session.

@available(iOS 9.0, *)
public class WatchData: NSObject,WCSessionDelegate {
var session = WCSession.defaultSession()

class var shared: WatchData {
    struct Static {
        static var onceToken: dispatch_once_t = 0
        static var instance: WatchData? = nil
    }
    dispatch_once(&Static.onceToken) {
        Static.instance = WatchData()
    }
    return Static.instance!
}

public func sessionWatchStateDidChange(session: WCSession) {
    print(__FUNCTION__)
    print(session)
    print("reachable:\(session.reachable)")
}

public func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    print(__FUNCTION__)
    guard message["request"] as? String == "showAlert" else {return}
    guard let m = message["m"] as? String else { return }
    print("msg:",m)
}


public func sendInbox(){
    var  payload:String = ""

    for (_, d0) in DATA.inboxDict
    {

        if let _ = d0["detnumber"]{
            let  dt = d0["detnumber"] as! String
            payload = payload + "|" + dt
        }

    }

    let message = [
        "m": payload
    ]
    if (!session.reachable){
        if WCSession.isSupported() {    //  it is supported
            session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()
            print("iphone activating WCSession")
        } else {

            print("iphone does not support WCSession")
        }

        session.activateSession()
    }


    if(!session.reachable){
        print("not reachable")
        return
    }else{

        let transfer:WCSessionUserInfoTransfer =  (session.transferUserInfo(["data" : message]) as WCSessionUserInfoTransfer?)!
        if(transfer.transferring){
            print("communicating from iphone")
        }else{
            print("not communicating from iphone")
        }
    }

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment