SettingsViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // SettingsViewController.swift
  3. // AIPaint
  4. //
  5. // Created by Fengyu He on 2022/11/29.
  6. //
  7. import UIKit
  8. import SnapKit
  9. import AuthenticationServices
  10. class SettingsViewController: UIViewController {
  11. let welcomeView: UIView = {
  12. var view = UIView()
  13. view.layer.borderWidth = 1
  14. view.layer.borderColor = UIColor.systemGray.cgColor
  15. view.layer.cornerRadius = 14
  16. return view
  17. }()
  18. lazy var welcomeText: UILabel = {
  19. var label = UILabel()
  20. label.numberOfLines = 0
  21. label.lineBreakMode = .byWordWrapping
  22. label.text = "感谢你参与画酱的封闭测试,见证画酱的诞生!\n你可以加入QQ群来对本 App 提出宝贵意见:390674039 \n此 App 目前还在初期稳定行测试阶段,如果你有什么功能上的需求,或是对于 App 的设计有任何的建议,欢迎进群与我讨论!\n \n暂时还没适配暗黑模式,请在正常模式使用此 App。"
  23. return label
  24. }()
  25. var signInWithAppleButton = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. if UITraitCollection.current.userInterfaceStyle == .dark {
  29. view.backgroundColor = .black
  30. } else {
  31. view.backgroundColor = .white
  32. }
  33. self.title = "设置"
  34. signInWithAppleButton.cornerRadius = 8.0
  35. signInWithAppleButton.addTarget(self, action: #selector(loginButtonTapped), for: .touchUpInside)
  36. view.addSubview(signInWithAppleButton)
  37. if #available(iOS 15.0, *) {
  38. signInWithAppleButton.isHidden = false
  39. } else {
  40. signInWithAppleButton.isHidden = true
  41. }
  42. view.addSubview(welcomeView)
  43. welcomeView.addSubview(welcomeText)
  44. welcomeView.snp.makeConstraints { (make) in
  45. make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(5)
  46. make.centerX.equalTo(view.safeAreaLayoutGuide.snp.centerX)
  47. make.width.equalTo(view.safeAreaLayoutGuide.snp.width).multipliedBy(0.95)
  48. make.height.equalTo(view.safeAreaLayoutGuide.snp.height).multipliedBy(0.35)
  49. }
  50. welcomeText.snp.makeConstraints { (make) in
  51. make.top.equalTo(welcomeView.snp.top).offset(5)
  52. make.centerX.equalTo(welcomeView.snp.centerX)
  53. make.width.equalTo(welcomeView.snp.width).multipliedBy(0.9)
  54. make.height.equalTo(welcomeView.snp.height).multipliedBy(0.98)
  55. }
  56. }
  57. @objc func loginButtonTapped() {
  58. if #available(iOS 15.0, *) {
  59. let provider = ASAuthorizationAppleIDProvider()
  60. let request = provider.createRequest()
  61. request.requestedScopes = [.email, .fullName]
  62. let controller = ASAuthorizationController(authorizationRequests: [request])
  63. controller.delegate = self
  64. // controller.presentationContextProvider = self
  65. controller.performRequests()
  66. }
  67. }
  68. }
  69. extension SettingsViewController: ASAuthorizationControllerDelegate {
  70. @available(iOS 15.0, *)
  71. func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
  72. do {
  73. //苹果用户唯一标识符,该值在同一个开发者账号下的所有 App 下是一样的,开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来。
  74. var credential = authorization.credential as! ASAuthorizationAppleIDCredential
  75. let userID = credential.user
  76. let fullName = credential.fullName
  77. let identifierToken = credential.identityToken
  78. }
  79. func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
  80. print("Login Error")
  81. }
  82. func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  83. return view.window!
  84. }
  85. }
  86. }
  87. //extension SettingsViewController: ASAuthorizationControllerPresentationContextProviding {
  88. // func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  89. //
  90. // }
  91. //}