如何解决如何使用 MethodChannels 将 Siri 集成到 Flutter 中?
我想将 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 文件如何调用 flutter 方法并用 Siri 响应?
解决方法
我遇到了类似的问题,但我能够让它发挥作用。
问题在于 Siri Intent 位于不同的 Target 中,因为它们可以在需要时在后台运行,因此您使用 Method 通道进行通信将非常复杂(如果可能)。
我的方法是使用颤振扩展 HomeWidget https://pub.dev/packages/home_widget
由于 Home 小部件也使用不同的目标,并且此扩展程序使用应用程序组和 UserDefaults 打开通信通道,我们可以使用相同的通信通道将数据发送到我们的 Siri Intent。
只需按照 HomeWidget 扩展的配置指南,将 AppGroups 功能添加到您的 runner Target 以及您的 Siri Intent。 确保您在所有这些中使用相同的组 ID,包括颤振侧:
HomeWidget.setAppGroupId('YOUR_GROUP_ID');
然后就可以使用扩展方法在flutter和swift之间收发数据了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。