Applewatchアプリ開発の勉強を始めたばかりの者です。
実装をしたのですが、方法がわからず悩んでおります。
AppleWatchからiphoneへデータを送る方法をサイトを参考に実装したのですが
(参考サイト様:https://qiita.com/koogawa/items/b39e1419cbbcda8efc07)
「iphoneにAppleWatchから送信したテキストを表示する」という部分がうまくいきません。
やりたいことはApplewatchのボタンをタップすると、iphone側にデータ(テキスト)を送り、
Applewatchから送信したテキストをiphoneの画面(ラベル)に表示したいです。
Watch側で送った"hoge" : "huga"を受け取ってwatchMessageLabelに表示したいのですが
方法がわかりません。
helloLabelには”メッセージを受け取りました”と画面に表示されるので、そこの処理は通っているのですが
メッセージを受け取って表示させる方法はどのように実装したら良いのでしょうか。
他に必要なメソッドがいるのでしょうか。
勉強不足で申し訳ありませんが、どなたかお知恵を頂きたいです。
宜しくお願い致します。
- ViewController.swift
import UIKit import WatchConnectivity class ViewController: UIViewController, WCSessionDelegate { //didReceiveMessageが通っているか確認 @IBOutlet weak var helloLabel: UILabel! //AppleWatchから送られるメッセージを表示 @IBOutlet weak var watchMessageLabel: UILabel! func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { print("activationDidCompleteWith") } func sessionDidBecomeInactive(_ session: WCSession) { print("sessionDidBecomeInactive") } func sessionDidDeactivate(_ session: WCSession) { print("sessionDidDeactivate") } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. if (WCSession.isSupported()) { let session = WCSession.default session.delegate = self session.activate() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) { replyHandler(["reply" : "OK"]) helloLabel.text = "メッセージを受け取りました" //Applewatchから送られるメッセージを表示したい //watchMessageLabel.text = } }
- InterfaceController.swift
import WatchKit import Foundation import WatchConnectivity class InterfaceController: WKInterfaceController, WCSessionDelegate { @IBOutlet var textlabel: WKInterfaceLabel! let wcSession = WCSession.default override func awake(withContext context: Any?) { super.awake(withContext: context) } override func didAppear() { super.didAppear() } override func willActivate() { super.willActivate() let session = WCSession.default session.delegate = self session.activate() } override func didDeactivate() { super.didDeactivate() } func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { let text = session.isReachable ? "reachable" : "unreachable" DispatchQueue.main.async { self.textlabel.setText(text) print("メインスレッドです") } } @IBAction func buttonTap() { if (WCSession.default.isReachable) { let message = ["hoge" : "huga"] WCSession.default.sendMessage(message, replyHandler: { (reply) -> Void in DispatchQueue.main.async { self.textlabel.setText("ok") } }, errorHandler: { (error) -> Void in DispatchQueue.main.async { self.textlabel.setText("error") } } ) } } }
実装環境
Xcode 10.0
Swift 4.2
AppleWatch series1
WatchOS 4.3.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/22 05:20