如何解决watchOS 按钮 onClick 触发 Flutter 功能
我刚刚开始使用 Swift,所以我真的不知道该怎么做。
我有标准的 Flutter begin 应用程序,flutter counter。我有一个 Apple Watch 的连接,我在手表上展示了计数器。当我按下 flutter 应用程序上的 + 按钮时,手表的数字也会增加。
现在我想要反过来。按下手表上的按钮可增加 flutter 应用程序上的数字。
这可能吗?现在我刚刚做了一些研究,因为我真的不知道从哪里开始。我按照本教程来获取手表上的计数器。 Tutorial
我在颤振中拥有什么:
static const channel = MethodChannel('MyTestAppChannel'); channel.invokeMethod("sendStringToNative",_counter.toString()); //In _incrementCounter()
我的 AppDelegate:
@objc class AppDelegate: FlutterAppDelegate,WCSessionDelegate {
func session(_ session: WCSession,activationDidCompleteWith activationState: WCSessionActivationState,error: Error?){}
func sessionDidBecomeInactive(_ session: WCSession) {}
func sessionDidDeactivate(_ session: WCSession) {}
func sendString(text: String){
print(text)
let session = WCSession.default;
if(session.isPaired && session.isReachable) {
DispatchQueue.main.async {
print("Sending counter")
session.sendMessage(["counter": text],replyHandler: nil)
}
} else {
print("Watch not reachable...")
}
}
override func application(
_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if(WCSession.isSupported()) {
let session = WCSession.default;
session.delegate = self;
session.activate();
}
let controller : FlutterViewController = window.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "MyTestAppChannel",binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({
(call: FlutterMethodCall,result: @escaping FlutterResult) -> Void in
if(call.method == "sendStringToNative") {
self.sendString(text: call.arguments as! String)
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application,didFinishLaunchingWithOptions: launchOptions)
}
}
我的界面控制器
class InterfaceController: WKInterfaceController,error: Error?) {}
@IBOutlet var label: WKInterfaceLabel!
override func awake(withContext context: Any?) {
// Configure interface objects here.
super.awake(withContext: context)
if(WCSession.isSupported()) {
let session = WCSession.default;
session.delegate = self;
session.activate();
}
label.setText("")
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func session(_ session: WCSession,didReceiveMessage message: [String: Any]) {
print(message)
label.setText((message["counter"] as! String))
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。