Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

55 řádky
1.8 KiB

  1. import Cocoa
  2. import FlutterMacOS
  3. public class PlatformDeviceIdPlugin: NSObject, FlutterPlugin {
  4. public static func register(with registrar: FlutterPluginRegistrar) {
  5. let channel = FlutterMethodChannel(name: "com.di1shuai.flutter/platform_device_id", binaryMessenger: registrar.messenger)
  6. let instance = PlatformDeviceIdPlugin()
  7. registrar.addMethodCallDelegate(instance, channel: channel)
  8. }
  9. /// 执行脚本命令
  10. ///
  11. /// - Parameters:
  12. /// - command: command content
  13. /// - needAuthorize: need sudo ?
  14. /// - Returns: result string
  15. public func runCommand(_ command: String, needAuthorize: Bool) -> String {
  16. let scriptWithAuthorization = """
  17. do shell script "\(command)" with administrator privileges
  18. """
  19. let scriptWithoutAuthorization = """
  20. do shell script "\(command)"
  21. """
  22. let script = needAuthorize ? scriptWithAuthorization : scriptWithoutAuthorization
  23. let appleScript = NSAppleScript(source: script)
  24. var error: NSDictionary? = nil
  25. let result = appleScript!.executeAndReturnError(&error)
  26. if let error = error {
  27. print("Error -> \n\(command)\n:")
  28. print(error)
  29. // return (false, nil)
  30. return ""
  31. }
  32. // return (true, result.stringValue)
  33. return result.stringValue!
  34. }
  35. public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  36. switch call.method {
  37. case "getPlatformVersion":
  38. result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
  39. case "getDeviceId":
  40. let command = String(#"ioreg -l | grep IOPlatformUUID | awk 'NR==1{print $4}' | sed 's/\"//g'"#)
  41. let deviceId = runCommand(command,needAuthorize: false);
  42. result(deviceId)
  43. default:
  44. result(FlutterMethodNotImplemented)
  45. }
  46. }
  47. }