Last active
April 25, 2018 17:14
-
-
Save charlesferreira/2fbf903ca5a758d897ae9aecc072d095 to your computer and use it in GitHub Desktop.
Watch connectivity example
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 UIKit | |
import WatchConnectivity | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
WCSession.default.delegate = self | |
WCSession.default.activate() | |
return true | |
} | |
} | |
extension AppDelegate: WCSessionDelegate { | |
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { | |
// TODO: Converter a mensagem para o tipo esperado (no exemplo, usei String) | |
guard let messageContent = message["mensagemDoWatchOSParaiOS"] as? String else { return } | |
print(messageContent) | |
} | |
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {} | |
func sessionDidBecomeInactive(_ session: WCSession) {} | |
func sessionDidDeactivate(_ session: WCSession) {} | |
} |
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 WatchKit | |
import WatchConnectivity | |
class ExtensionDelegate: NSObject, WKExtensionDelegate { | |
func applicationDidFinishLaunching() { | |
WCSession.default.delegate = self | |
WCSession.default.activate() | |
} | |
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) { | |
// ... | |
} | |
} | |
extension ExtensionDelegate: WCSessionDelegate { | |
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {} | |
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { | |
// TODO: Converter a mensagem para o tipo esperado (no exemplo, usei String) | |
guard let messageContent = message["mensagemDoiOSParaWatchOS"] as? String else { return } | |
print(messageContent) | |
} | |
} |
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
// Exemplo no lado do WatchOS | |
import WatchConnectivity | |
class InterfaceController: WKInterfaceController { | |
override func willActivate() { | |
super.willActivate() | |
let message = ["mensagemDoWatchOSParaiOS": "Tchau, cara de pau"] | |
WCSession.default.sendMessage(message, replyHandler: nil, errorHandler: nil) | |
} | |
} |
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
// Exemplo no lado do iOS | |
import UIKit | |
import WatchConnectivity | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let message = ["mensagemDoiOSParaWatchOS": "Oi, cara de boi"] | |
WCSession.default.sendMessage(message, replyHandler: nil, errorHandler: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment