24.调用原生相册
MethodChannel
示例代码
定义一个Channel
MethodChannel _channel = const MethodChannel('mine_header/channel');给原生发送指令
_channel.invokeMapMethod('sel_avatar');原生处理指令
import UIKit
import Flutter
/// 这里将指令用 枚举包装,便于使用
enum MineChannelMethod: String {
case AvatarPick = "sel_avatar"
}
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if let flutterVC = window?.rootViewController as? FlutterViewController,
let messenger = flutterVC as? FlutterBinaryMessenger
{
// 实例化 channel
let mineChannel = FlutterMethodChannel.init(name: "mine_header/channel", binaryMessenger: messenger)
mineChannel.setMethodCallHandler { call, result in
guard
call.method.isEmpty == false,
let method = MineChannelMethod(rawValue: call.method)
else {
return
}
switch method {
case .AvatarPick:
let picker = UIImagePickerController()
picker.modalPresentationStyle = .overFullScreen
picker.delegate = self
flutterVC.present(picker, animated: true, completion: nil)
}
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
extension AppDelegate: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}检验效果

数据传递
数据展示

Last updated