watchOS 按钮 onClick 触发 Flutter 功能

时间:2021-06-03 11:59:26

标签: ios swift flutter watchkit apple-watch

我刚刚开始使用 Swift,所以我真的不知道该怎么做。

我有标准的 Flutter begin 应用程序,flutter counter。我有一个 Apple Watch 的连接,我在手表上展示了计数器。当我按下 flutter 应用程序上的 + 按钮时,手表的数字也会增加。

现在我想要反过来。按下手表上的按钮可增加 flutter 应用程序上的数字。

这可能吗?现在我刚刚做了一些研究,因为我真的不知道从哪里开始。我按照本教程来获取手表上的计数器。 Tutorial

What I have now

我在颤振中拥有什么:

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, WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, 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))
   }
}

0 个答案:

没有答案