方法通道
在 Android 中是通过 FlutterView,而在 iOS 中则是通过 FlutterViewController 进行注册的。FlutterView 与 FlutterViewController 为 Flutter 应用提供了一个画板,使得构建于 Skia 之上的 Flutter 通过绘制即可实现整个应用所需的视觉效果。
Flutter 代码
const platform =MethodChannel('samples.chenhang/navigation');// 声明 MethodChannel
class DefaultPage extends StatelessWidget {
DefaultPage({Key? key}) :super(key: key);
String _nativeCallBackValue ='等待原生传值';
//异步执行调用原生方法,保持页面不卡住,因为调用原生的方法可能没实现会抛出异常,所以trycatch包住
Future_communicateFunction(flutterPara)async {
try {
//原生方法名为callNativeMethond,flutterPara为flutter调用原生方法传入的参数,await等待方法执行
final result =await platform.invokeMethod(
'callNativeMethond', flutterPara);
//如果原生方法执行回调传值给flutter,那下面的代码才会被执行
_nativeCallBackValue =result;
print("----$_nativeCallBackValue");
}on PlatformException catch (e) {
//抛出异常
//flutter: PlatformException(001, 进入异常处理, 进入flutter的trycatch方法的catch方法)
print("----$e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor:Colors.yellowAccent,
appBar:AppBar(title:Text("Default Page")),
body:Center(
child:
RaisedButton(child:Text("打开应用商店"), onPressed: () {
_communicateFunction({"type":"221133"});
// platform.invokeMethod('callNativeMethond');
})),
);
}
}
iOS端代码
在 iOS 平台,方法调用的处理和响应是在 Flutter 应用的入口,也就是在 Applegate 中的 rootViewController(即 FlutterViewController)里实现的,因此我们需要打开 Flutter 的 iOS 宿主 App,找到 AppDelegate.m 文件,并添加相关逻辑。
override func application(
_ application:UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) ->Bool {
GeneratedPluginRegistrant.register(with:self)
let controller = self.window.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel.init(name: "samples.chenhang/navigation", binaryMessenger: controller as! FlutterBinaryMessenger)
channel.setMethodCallHandler { (call, result) in
if call.method == "callNativeMethond" {
letstr ="itms-apps://itunes.apple.com/xy/app/foo/id414478124"
guard leturl = URL(string: str)else {return }
letcan = UIApplication.shared.canOpenURL(url)
ifcan {
if #available(iOS 10.0,*) {
UIApplication.shared.open(url, options: [:]) { (b)in
print("打开结果: \(b)")
}
}else {
//iOS 10 以前
UIApplication.shared.openURL(url)
}
}
result("我是原生返回数据----")
print("111111")
letpara = call.arguments
print("------\(para!)")
}else{
print("22222222")
result(FlutterMethodNotImplemented)
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Android端 代码 在 Android 平台,方法调用的处理和响应是在 Flutter 应用的入口,也就是在 MainActivity 中的 FlutterView 里实现的,因此我们需要打开 Flutter 的 Android 宿主 App,找到 MainActivity.java 文件,并在其中添加相关的逻辑。
android/app/src/main/java/com/example/MainActivity.java 文件
//import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class MainActivityextends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),"samples.chenhang/navigation").setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
// Note: this method is invoked on the main thread.
if(call.method.equals("callNativeMethond")) {
try {
Uri uri = Uri.parse("market://details?id=com.tencent.mm");
Intent intent =new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (Exception e) {
// result.error("UNAVAILABLE", "没有安装应用市场", null);
}
Log.e("MainActivity","Flutter -> Android 回调内容:" + call.arguments.toString());
result.success("Android -> Flutter 接收回调后返回值:" +"Na收到指令");
}
else {
result.notImplemented();
}
}
});
}
}