Files
record-tool/ios/Runner/PlatformInfoPlugin.swift
林锋 02c1c87b46 1. 升级依赖版本
2. 解决运行项目警告日志问题
3. 优化代码
2026-06-04 13:55:33 +08:00

72 lines
2.1 KiB
Swift

import Flutter
import UIKit
final class PlatformInfoPlugin: NSObject, FlutterPlugin {
static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
name: "com.gdfw.fxjk/platform_info",
binaryMessenger: registrar.messenger()
)
let plugin = PlatformInfoPlugin()
registrar.addMethodCallDelegate(plugin, channel: channel)
}
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "packageInfo":
result(packageInfoMap())
case "deviceInfo":
result(deviceInfoMap())
default:
result(FlutterMethodNotImplemented)
}
}
private func packageInfoMap() -> [String: String] {
let bundle = Bundle.main
return [
"appName": displayName(bundle: bundle),
"packageName": bundle.bundleIdentifier ?? "",
"version": bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "",
"buildNumber": bundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "",
]
}
private func deviceInfoMap() -> [String: Any] {
let device = UIDevice.current
return [
"platform": "ios",
"brand": device.systemName,
"model": machineIdentifier(),
"systemVersion": device.systemVersion,
"isPhysicalDevice": !isSimulator(),
]
}
private func displayName(bundle: Bundle) -> String {
if let displayName = bundle.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String {
return displayName
}
return bundle.object(forInfoDictionaryKey: "CFBundleName") as? String ?? ""
}
private func isSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
private func machineIdentifier() -> String {
var systemInfo = utsname()
uname(&systemInfo)
let mirror = Mirror(reflecting: systemInfo.machine)
let identifier = mirror.children.reduce(into: "") { value, element in
guard let byte = element.value as? Int8, byte != 0 else { return }
value.append(String(UnicodeScalar(UInt8(byte))))
}
return identifier
}
}