AppDelegate.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import UIKit
  2. import Flutter
  3. import WebKit
  4. @UIApplicationMain
  5. @objc class AppDelegate: FlutterAppDelegate {
  6. let flutterChannelName = "o2oa.net/flutter_inner"
  7. override func application(
  8. _ application: UIApplication,
  9. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  10. ) -> Bool {
  11. // 插件内部调用
  12. let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
  13. let batteryChannel = FlutterMethodChannel(name: flutterChannelName,
  14. binaryMessenger: controller.binaryMessenger)
  15. batteryChannel.setMethodCallHandler({
  16. (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
  17. self.handle(call: call, result: result, controller: controller)
  18. })
  19. //
  20. GeneratedPluginRegistrant.register(with: self)
  21. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  22. }
  23. private func handle(call: FlutterMethodCall, result: @escaping FlutterResult, controller : FlutterViewController ) {
  24. // 参数
  25. let args = call.arguments as? [String:Any]
  26. switch call.method {
  27. case "openFile": // 打开文档
  28. print("打开文件的方法 ios!")
  29. if let filePath = (args?["filePath"] as? String) {
  30. let shareBtnShow = (args?["shareBtnShow"] as? Bool) ?? true
  31. let doneTitle = (args?["doneTitle"] as? String) ?? "关闭"
  32. let openFile = OpenFilePlugin()
  33. openFile.handleMethodCallOpenFile(filePath: filePath, result: result, shareBtnShow: shareBtnShow, doneTitle: doneTitle)
  34. } else {
  35. result("没有传入filePath")
  36. }
  37. break
  38. case "clearCache":
  39. print("清除缓存 ios!")
  40. self.clearCache(result: result)
  41. break
  42. case "checkRoot":
  43. print("越狱检查 ios!")
  44. self.checkRoot(result: result)
  45. break
  46. case "saveToAlbum":
  47. print("保存到相册")
  48. if let filePath = (args?["filePath"] as? String), !filePath.isEmpty {
  49. self.saveToAlbum(filePath: filePath, result: result)
  50. } else {
  51. result("没有传入filePath")
  52. }
  53. break
  54. default :
  55. result(FlutterMethodNotImplemented)
  56. break
  57. }
  58. }
  59. private func saveToAlbum(filePath: String, result: @escaping FlutterResult) {
  60. let currentURL = NSURL(fileURLWithPath: filePath)
  61. if let url = currentURL.path {
  62. UIImageWriteToSavedPhotosAlbum(UIImage(contentsOfFile: url)!, self, #selector(self.saveImage(image:didFinishSavingWithError:contextInfo:)), nil)
  63. }
  64. result(true)
  65. }
  66. @objc func saveImage(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafeRawPointer) {
  67. if let e = error {
  68. print(e.localizedDescription)
  69. } else {
  70. print("保存图片到相册成功!")
  71. }
  72. }
  73. /// 越狱检查
  74. private func checkRoot(result: @escaping FlutterResult) {
  75. var checked = false
  76. #if !targetEnvironment(simulator)
  77. print("不是模拟器啦。。。。。")
  78. //越狱检查
  79. checked = SecurityCheckManager.shared.isJailBroken()
  80. #endif
  81. result(checked)
  82. }
  83. let group = DispatchGroup()
  84. private func clearCache(result: @escaping FlutterResult) {
  85. self.group.enter()
  86. DispatchQueue.main.async(group: self.group, execute: DispatchWorkItem(block: {
  87. let types = WKWebsiteDataStore.allWebsiteDataTypes()
  88. WKWebsiteDataStore.default().removeData(ofTypes: types, modifiedSince: Date(timeIntervalSince1970: 0), completionHandler: {
  89. print("浏览器缓存清除")
  90. self.group.leave()
  91. })
  92. }))
  93. self.group.notify(queue: DispatchQueue.main) {
  94. result("success")
  95. }
  96. }
  97. }