Flutter中点击button按钮如何启动iOS/Android设备自带的系统笔记应用?
Flutter 双端打开系统自带笔记应用实现方案
实现该需求目前有两种成熟可行的方案,分别对应快速实现和高兼容性场景:
方案1:基于 url_launcher 包的协议跳转(实现成本最低)
利用系统自带的URL Scheme/Intent协议直接跳转,不需要编写原生代码:
- 先在
pubspec.yaml中引入依赖:
dependencies: url_launcher: ^6.2.0
- 双端跳转逻辑实现:
import 'dart:io'; import 'package:url_launcher/url_launcher.dart'; Future<void> openSystemNoteApp() async { if (Platform.isAndroid) { // 适配绝大多数安卓系统的笔记应用跳转Intent final Uri noteIntent = Uri.parse( 'intent://#Intent;action=android.intent.action.MAIN;category=android.intent.category.APP_NOTES;end' ); if (await canLaunchUrl(noteIntent)) { await launchUrl(noteIntent); } else { // 异常处理:无匹配的笔记应用 } } else if (Platform.isIOS) { // iOS系统备忘录默认URL Scheme final Uri noteScheme = Uri.parse('mobilenotes://'); if (await canLaunchUrl(noteScheme)) { await launchUrl(noteScheme); } else { // 异常处理:无匹配的笔记应用 } } }
- 权限配置:
- iOS端需要在
ios/Runner/Info.plist中添加白名单配置,否则无法检测跳转可用性:
<key>LSApplicationQueriesSchemes</key> <array> <string>mobilenotes</string> </array>
注意:部分深度定制的安卓ROM可能修改了系统笔记的协议,存在小概率不兼容的情况,对兼容性要求高可以选方案2
方案2:自定义Method Channel调用原生逻辑(兼容性最高)
通过Flutter渠道调用双端原生API实现跳转,适配所有主流系统版本:
- Flutter端调用通道定义:
import 'package:flutter/services.dart'; static const _channel = MethodChannel('com.your_package_name/open_note'); Future<void> openSystemNote() async { try { await _channel.invokeMethod('openSystemNote'); } on PlatformException catch (_) { // 异常处理:无匹配的笔记应用 } }
- Android端原生实现(Kotlin):
在android/app/src/main/kotlin/.../MainActivity.kt中添加通道处理逻辑:
import android.content.Intent import android.content.ActivityNotFoundException import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() { private val CHANNEL = "com.your_package_name/open_note" override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> if (call.method == "openSystemNote") { val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_APP_NOTES) flags = Intent.FLAG_ACTIVITY_NEW_TASK } try { startActivity(intent) result.success(true) } catch (e: ActivityNotFoundException) { result.error("NO_NOTE_APP", "No system note application installed", null) } } else { result.notImplemented() } } } }
- iOS端原生实现(Swift):
在ios/Runner/AppDelegate.swift中添加通道处理逻辑:
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { let CHANNEL = "com.your_package_name/open_note" let controller : FlutterViewController = window?.rootViewController as! FlutterViewController FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger).setMethodCallHandler { call, result in if call.method == "openSystemNote" { guard let url = URL(string: "mobilenotes://") else { result.error("INVALID_URL", "Invalid note app scheme", nil) return } if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) result.success(true) } else { result.error("NO_NOTE_APP", "No system note application installed", nil) } } else { result(FlutterMethodNotImplemented) } } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
两种方案都建议补充异常兜底逻辑,比如检测到没有预装笔记应用时,弹出提示引导用户手动安装第三方笔记应用。
内容的提问来源于stack exchange,提问作者Ben Ajax
相关产品推荐
相关产品推荐

