45 lines
1.7 KiB
Swift
45 lines
1.7 KiB
Swift
import Flutter
|
||
import UIKit
|
||
|
||
@main
|
||
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
|
||
override func application(
|
||
_ application: UIApplication,
|
||
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
||
) -> Bool {
|
||
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||
}
|
||
|
||
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
|
||
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
|
||
// 拨号 MethodChannel:与 Android 侧保持一致(com.ttstd.familycare/app)
|
||
guard let registrar = engineBridge.pluginRegistry.registrar(forPlugin: "PhoneDialer") else {
|
||
return
|
||
}
|
||
let channel = FlutterMethodChannel(
|
||
name: "com.ttstd.familycare/app",
|
||
binaryMessenger: registrar.messenger()
|
||
)
|
||
channel.setMethodCallHandler { call, result in
|
||
if call.method == "dialPhone" {
|
||
guard let args = call.arguments as? [String: Any],
|
||
let phone = args["phone"] as? String else {
|
||
result(FlutterError(code: "INVALID_PHONE", message: "电话号码为空", details: nil))
|
||
return
|
||
}
|
||
// 仅保留数字与 +,避免空格/括号等字符导致 URL 解析失败
|
||
let cleaned = phone.filter { $0.isNumber || $0 == "+" }
|
||
guard !cleaned.isEmpty, let url = URL(string: "tel:\(cleaned)") else {
|
||
result(FlutterError(code: "INVALID_PHONE", message: "电话号码为空", details: nil))
|
||
return
|
||
}
|
||
UIApplication.shared.open(url) { success in
|
||
result(success)
|
||
}
|
||
} else {
|
||
result(FlutterMethodNotImplemented)
|
||
}
|
||
}
|
||
}
|
||
}
|