Last active
August 29, 2015 14:28
-
-
Save johndpope/d07e7df2986461276a9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// this is used targeting to my iphone - targeting
in my view controller I'm sending data by invoking this
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()
}