【发布时间】: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