ios - 在 Flutter 中使用平台特定代码时如何在 App Delegate 中实现协议委托模式?
问题描述
我想在 iOS 上为我的 Flutter 应用程序实现特定于平台的代码。我想从原生 iOS 实现 EventKit。但是现在我很困惑如何在 Flutter 中使用 Platform Channel 时在应用程序委托中实现协议委托模式。
在本机 iOS 中,我的代码将是这样的
import EventKitUI
class MyViewController : UIViewController, EKEventEditViewDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonPressed(_ sender: Any) {
// after pressing a button then show a VC
showEventKitViewController()
}
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
// this is the method to conform the protocol
dismiss(animated: true, completion: nil)
}
func showEventKitViewController() {
let eventVC = EKEventEditViewController()
eventVC.editViewDelegate = self // I am confused in this line
eventVC.eventStore = EKEventStore()
let event = EKEvent(eventStore: eventVC.eventStore)
eventVC.event = event
present(eventVC, animated: true)
}
}
如您所见,我将self
( MyViewController 类作为委托)分配给editViewDelegate
现在我很困惑如何showEventKitViewController
在下面的 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 channel = FlutterMethodChannel(name: "useOtherApp", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({ (call: FlutterMethodCall, result: FlutterResult) -> Void in
if (call.method == "saveToEventKit") {
// what should I do in here to get the same result like my code above in native?
}
})
}
特别是当我需要分配一个类作为这样的代表editViewDelegate
时
解决方案
我可以通过使用扩展来使用这样的代码来解决它。请滚动到最底部以查看 FlutterViewController 扩展
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
setUpMethodChannels(controller: controller) // set the method channel
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func setUpMethodChannels(controller: FlutterViewController) {
let channel = FlutterMethodChannel(name: "useOtherApp", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({ (call: FlutterMethodCall, result: FlutterResult) -> Void in
guard let args = call.arguments as? [String : Any] else {return}
if (call.method == "saveToCalendar") {
let eventVC = EKEventEditViewController()
eventVC.editViewDelegate = controller // set FlutterViewController as the delegate
eventVC.eventStore = EKEventStore()
let event = EKEvent(eventStore: eventVC.eventStore)
eventVC.event = event
eventVC.title = "hallooooo"
controller.present(eventVC, animated: true)
} else {
result(FlutterMethodNotImplemented)
}
})
}
}
// set the extension like below OUTSIDE the AppDelegate class
extension FlutterViewController : EKEventEditViewDelegate, UINavigationControllerDelegate {
// conform to the EKEventEditViewDelegate protocol
public func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
dismiss(animated: true, completion: nil)
}
}
推荐阅读
- sql-server-2017 - 标准版 Linux 上 SQL Server 2017 的透明数据库加密
- netsuite - 从 netsuite 脚本 2.0 发送电子邮件时更改电子邮件的昵称
- ios - 将音频数据发送到服务器后单击噪声
- arangodb - 从 arangodb 中包含超过 1 个数组的结果中获取公共元素
- qr-code - 使用 Segno 即时生成 qrcode
- rx-swift - RXSwift act while debounced
- arrays - 双和优化
- driver - 有关已签名 Windows 驱动程序的详细信息
- php - 如何在 magento 2 中隐藏错误通知消息
- angular - angular 6从孙子执行祖父函数