Last active
June 21, 2018 10:43
-
-
Save litoarias/3542d71b3b319b3a51eb8e2572d2bd7b to your computer and use it in GitHub Desktop.
Tutorial Watch 12
This file contains hidden or 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 UIKit | |
import WatchConnectivity | |
class ViewController: UIViewController { | |
var connectivityHandler = WatchSessionManager.shared | |
var counter = 0 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
connectivityHandler.iOSDelegate = self | |
} | |
/// Send messages on main thread | |
/// | |
/// - Parameter sender: UIButton | |
@IBAction func sendMessage(_ sender: UIButton) { | |
counter += 1 | |
connectivityHandler.sendMessage(message: ["msg" : "Message \(counter)" as AnyObject]) { (error) in | |
print("Error sending message: \(error)") | |
} | |
} | |
} | |
extension ViewController: iOSDelegate { | |
func messageReceived(tuple: MessageReceived) { | |
// Handle receiving message | |
guard let reply = tuple.replyHandler else { | |
return | |
} | |
// Need reply to counterpart | |
switch tuple.message["request"] as! RequestType.RawValue { | |
case RequestType.date.rawValue: | |
reply(["date" : "\(Date())"]) | |
case RequestType.version.rawValue: | |
let version = ["version" : "\(Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "No version")"] | |
reply(["version" : version]) | |
default: | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment