AppDelegate.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import UIKit
  2. import AppTrackingTransparency
  3. import Flutter
  4. import AuthenticationServices
  5. @main
  6. @objc class AppDelegate: FlutterAppDelegate {
  7. var channel:FlutterMethodChannel!
  8. var appleLoginResultBlock:((Dictionary<String, String>)->())!
  9. override func application(
  10. _ application: UIApplication,
  11. didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  12. ) -> Bool {
  13. GeneratedPluginRegistrant.register(with: self)
  14. self.initPlatformMethods()
  15. return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  16. }
  17. override func applicationDidBecomeActive(_ application: UIApplication) {
  18. if #available(iOS 14, *) {
  19. ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
  20. })
  21. }
  22. UIApplication.shared.applicationIconBadgeNumber = 0;
  23. }
  24. func initPlatformMethods(){
  25. self.channel = FlutterMethodChannel.init(name: "com.room/opapp", binaryMessenger: self.window.rootViewController as! FlutterBinaryMessenger)
  26. self.channel.setMethodCallHandler { call, result in
  27. if (call.method == "checkFace"){
  28. result(self.checkFace(path: call.arguments as! String));
  29. }else if (call.method == "appleLogin"){
  30. self.appleLogin()
  31. self.appleLoginResultBlock = { dic in
  32. result(dic);
  33. }
  34. }else if (call.method == "getNativeRouterName"){
  35. result("");
  36. }else if (call.method == "skipAndroidSplashScreen"){
  37. result(true);
  38. }
  39. }
  40. }
  41. func checkFace(path:String) -> Int{
  42. var image = CIImage.init(image: .init(contentsOfFile: path)!)
  43. var detector = CIDetector.init(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
  44. var features = detector!.features(in: image!);
  45. return features.count;
  46. }
  47. func appleLogin() {
  48. if #available(iOS 13.0, *) {
  49. let appleIDProvider = ASAuthorizationAppleIDProvider()
  50. let request = appleIDProvider.createRequest()
  51. request.requestedScopes = [.fullName, .email] // 可根据需求选择请求的范围
  52. let authorizationController = ASAuthorizationController(authorizationRequests: [request])
  53. authorizationController.delegate = self
  54. authorizationController.presentationContextProvider = self
  55. authorizationController.performRequests()
  56. } else {
  57. // Fallback on earlier versions
  58. }
  59. }
  60. }
  61. @available(iOS 13.0, *)
  62. extension AppDelegate: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
  63. func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  64. return UIApplication.shared.windows.filter({ $0.isKeyWindow }).last!
  65. }
  66. func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
  67. if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
  68. let identityToken = String.init(data: appleIDCredential.identityToken!, encoding: .utf8) ?? ""
  69. // let authorizationCode = String.init(data: appleIDCredential.authorizationCode!, encoding: .utf8) ?? ""
  70. self.appleLoginResultBlock(["identityToken":identityToken,"authorizationCode":appleIDCredential.user,"error":""])
  71. }else{
  72. self.appleLoginResultBlock(["error":"授权信息不符"]);
  73. }
  74. }
  75. func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
  76. // 处理登录错误
  77. // self.appleLoginResultBlock(["error":error.localizedDescription])
  78. var msg = "未知错误,请稍后重试";
  79. switch error {
  80. case ASAuthorizationError.unknown:
  81. //未知错误
  82. break;
  83. case ASAuthorizationError.canceled:
  84. //用户取消
  85. msg = "";
  86. case ASAuthorizationError.failed:
  87. msg = "授权请求失败";
  88. case ASAuthorizationError.invalidResponse:
  89. msg = "授权请求无响应";
  90. case ASAuthorizationError.notHandled:
  91. msg = "授权请求未处理";
  92. default:
  93. break;
  94. }
  95. self.appleLoginResultBlock(["error":msg])
  96. }
  97. }