【问题标题】:How to integrate Siri into Flutter with MethodChannels?如何通过 MethodChannels 将 Siri 集成到 Flutter 中?
【发布时间】:2021-06-03 12:23:38
【问题描述】:

我想将 Sirikit 集成到我的 Flutter 应用程序中。因此,我必须建立一个 MethodChannel 用于主机和客户端之间的通信。我希望 iOS 端调用一个颤振函数,该响应是 Siris 的答案。 这是扑面:

void nativeFunc() {
  const platform = const MethodChannel("flutter.siri");

  Future<dynamic> _handleMethod(MethodCall call) async {
    if (call.method == "message") {
      return api_request("Physics", call.arguments, 50);
    }
  }

  platform.setMethodCallHandler(_handleMethod);
}

那我还要在iOS端指定methodChannel:

//AppDelegate.swift
import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let methodChannel = FlutterMethodChannel(name:"flutter.siri")

    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

//IntentHandler.swift
    func handle(intent: INSendMessageIntent, completion: @escaping (INSendMessageIntentResponse) -> Void) {
        // Implement your application logic to send a message here.
        
        let userActivity = NSUserActivity(activityType: NSStringFromClass(INSendMessageIntent.self))
        let response = methodChannel.invokeMethod()//this should invoke the MethodChannel-function from Flutter
        
        
        completion(response)
    }

我不是 iOS 开发人员,所以我不知道如何实际执行此操作。现在的问题是 IntentHandler.swift-File 无法访问方法通道数据,因为它是在另一个文件中声明的。我的 Intenthandler-file 如何调用 flutter 方法并用 Siri 响应?

【问题讨论】:

    标签: ios swift flutter sirikit flutter-method-channel


    【解决方案1】:

    我遇到了类似的问题,我能够让它工作。

    问题在于 Siri Intent 位于不同的 Target 中,因为它们可以在需要时在后台运行,因此使用 Method 通道进行通信将非常复杂(如果可能的话)。

    我的方法是使用颤振扩展 HomeWidget https://pub.dev/packages/home_widget

    由于 Home 小部件也使用不同的目标,并且此扩展程序使用 App 组和 UserDefaults 打开了一个通信通道,我们可以使用相同的通信通道将数据发送到我们的 Siri Intent。

    只需按照 HomeWidget 扩展的配置指南,将 AppGroups 功能添加到您的跑步者目标和您的 Siri 意图。 确保在所有这些中使用相同的组 ID,包括在颤振方面:

    HomeWidget.setAppGroupId('YOUR_GROUP_ID');
    

    那么你就可以使用扩展方法在flutter和swift之间发送和接收数据了。

    【讨论】:

    • 你有从 swift 意图发送数据到颤动的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2021-12-12
    • 2020-09-22
    • 2021-05-16
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多