63 lines
2.4 KiB
Swift
63 lines
2.4 KiB
Swift
import Flutter
|
||
import Photos
|
||
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 if call.method == "saveRecordingToPhotos" {
|
||
guard let path = call.arguments as? String,
|
||
FileManager.default.fileExists(atPath: path) else {
|
||
result(FlutterError(code: "INVALID_RECORDING", message: "录制文件不存在", details: nil))
|
||
return
|
||
}
|
||
PHPhotoLibrary.shared().performChanges({
|
||
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: path))
|
||
}) { success, error in
|
||
DispatchQueue.main.async {
|
||
if success {
|
||
result(nil)
|
||
} else {
|
||
result(FlutterError(code: "SAVE_RECORDING_FAILED", message: error?.localizedDescription, details: nil))
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
result(FlutterMethodNotImplemented)
|
||
}
|
||
}
|
||
}
|
||
}
|