QSLContactAddAlertView.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // QSLContactAddAlertView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/12.
  6. //
  7. import UIKit
  8. import Contacts
  9. import ContactsUI
  10. class QSLContactAddAlertView: UIView {
  11. var vc: UIViewController?
  12. lazy var contentView: UIView = {
  13. let contentView = UIView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW - 60.rpx, height: 152.0.rpx))
  14. contentView.backgroundColor = .white
  15. contentView.addRadius(radius: 8.rpx)
  16. return contentView
  17. }()
  18. lazy var titleLabel: UILabel = {
  19. let label = UILabel()
  20. label.text("添加紧急联系人")
  21. label.mediumFont(17)
  22. label.textColor = QSLColor.Color_202020
  23. return label
  24. }()
  25. lazy var addInputView: UIView = {
  26. let view = UIView()
  27. view.backgroundColor = QSLColor.backGroundColor
  28. view.addRadius(radius: 4.rpx)
  29. view.addBorder(borderWidth: 1.rpx, borderColor: .hexStringColor(hexString: "#F2F2F2"))
  30. return view
  31. }()
  32. lazy var phoneTextField: UITextField = {
  33. let textField = UITextField()
  34. textField.maxTextNumber = 11
  35. textField.keyboardType = .numberPad
  36. textField.textColor = QSLColor.Color_202020
  37. textField.font = UIFont.textF(16)
  38. textField.placeholder = "请输入手机号"
  39. textField.setPlaceholderAttribute(font: UIFont.textF(16), color: UIColor.hexStringColor(hexString: "#A7A7A7"))
  40. return textField
  41. }()
  42. lazy var lineView: UIView = {
  43. let view = UIView()
  44. view.backgroundColor = .hexStringColor(hexString: "#F2F2F2")
  45. return view
  46. }()
  47. lazy var addressBookBtn: UIButton = {
  48. let btn = UIButton()
  49. btn.image(UIImage(named: "add_address_btn"))
  50. btn.title("通讯录导入")
  51. btn.textColor(.hexStringColor(hexString: "#11B58F"))
  52. btn.font(13)
  53. btn.addTarget(self, action: #selector(addressBtnAction), for: .touchUpInside)
  54. return btn
  55. }()
  56. lazy var firstButton: UIButton = {
  57. let btn = UIButton()
  58. btn.backgroundColor = .hexStringColor(hexString: "#F8F8F8")
  59. btn.addRadius(radius: 20.rpx)
  60. btn.title("取消")
  61. btn.textColor(.hexStringColor(hexString: "#A7A7A7"))
  62. btn.mediumFont(16)
  63. btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
  64. return btn
  65. }()
  66. lazy var secondButton: UIButton = {
  67. let btn = UIButton()
  68. btn.addRadius(radius: 20.rpx)
  69. btn.title("添加")
  70. btn.textColor(.white)
  71. btn.mediumFont(16)
  72. btn.gradientBackgroundColor(color1: .hexStringColor(hexString: "#15CBA1"), color2: .hexStringColor(hexString: "#1FE0BA"), width: 118.rpx, height: 40.rpx, direction: .horizontal)
  73. btn.addTarget(self, action: #selector(addBtnAction), for: .touchUpInside)
  74. return btn
  75. }()
  76. lazy var closeButton: UIButton = {
  77. let btn = UIButton()
  78. btn.setBackgroundImage(UIImage(named: "public_btn_close_AAA"), for: .normal)
  79. btn.addTarget(self, action: #selector(removeView), for: .touchUpInside)
  80. return btn
  81. }()
  82. var secondBtnClosure: ((String) -> ())?
  83. var addressBtnClosure: (() -> ())?
  84. class func alert(vc: UIViewController,
  85. secondBtnClosure: @escaping (String) -> ()) {
  86. let window = QSLContactAddAlertView(frame: CGRect(x: 0, y: 0, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
  87. window.vc = vc
  88. window.secondBtnClosure = secondBtnClosure
  89. vc.view.addSubview(window)
  90. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) {
  91. window.backgroundColor = .hexStringColor(hexString: "#000000", alpha: 0.7)
  92. window.contentView.isHidden = false
  93. }
  94. }
  95. override init(frame: CGRect) {
  96. super.init(frame: frame)
  97. initView()
  98. }
  99. required init?(coder: NSCoder) {
  100. fatalError("init(coder:) has not been implemented")
  101. }
  102. @objc func addBtnAction() {
  103. gravityInstance?.track(QSLGravityConst.contact_add)
  104. guard let text = self.phoneTextField.text, text.count == 11 else {
  105. self.vc?.view.toast(text: "请输入正确的电话号码")
  106. return
  107. }
  108. if let secondBtnClosure = self.secondBtnClosure {
  109. secondBtnClosure(text)
  110. }
  111. self.keyboardEndEditing()
  112. self.removeView()
  113. }
  114. @objc func addressBtnAction() {
  115. gravityInstance?.track(QSLGravityConst.contact_addressbook)
  116. self.removeView()
  117. let status = CNContactStore.authorizationStatus(for: .contacts)
  118. if status != .authorized {
  119. let store = CNContactStore()
  120. store.requestAccess(for: .contacts) { granted, error in
  121. if granted {
  122. DispatchQueue.main.async {
  123. let picker = CNContactPickerViewController()
  124. picker.delegate = self
  125. self.vc?.present(picker, animated: true, completion: nil)
  126. }
  127. } else {
  128. DispatchQueue.main.async {
  129. self.vc?.view.toast(text: "请先开启通讯录权限")
  130. }
  131. }
  132. }
  133. } else {
  134. let picker = CNContactPickerViewController()
  135. picker.delegate = self
  136. self.vc?.present(picker, animated: true, completion: nil)
  137. }
  138. }
  139. // 移除
  140. @objc func removeView() {
  141. UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 0.05) { [weak self] in
  142. self?.backgroundColor = UIColor.init(white: 0, alpha: 0)
  143. self?.contentView.isHidden = true
  144. } completion: { [weak self] finished in
  145. self?.removeFromSuperview()
  146. }
  147. }
  148. }
  149. extension QSLContactAddAlertView: CNContactPickerDelegate {
  150. // 通讯录选择电话
  151. func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
  152. let phoneArray = contact.phoneNumbers
  153. if let firstPhone = phoneArray.first {
  154. let phoneNum = firstPhone.value
  155. let code = phoneNum.stringValue
  156. let phone = code.replacingOccurrences(of: " ", with: "")
  157. self.phoneTextField.text = phone
  158. print("Phone number is --- \(code)")
  159. }
  160. let name = CNContactFormatter.string(from: contact, style: .fullName) ?? "Unknown"
  161. print("Full name is --- \(name)")
  162. }
  163. }
  164. extension QSLContactAddAlertView {
  165. func initView() {
  166. addSubview(contentView)
  167. contentView.snp.makeConstraints { make in
  168. make.size.equalTo(CGSize(width: QSLConst.qsl_kScreenW - 60.rpx, height: 203.0.rpx))
  169. make.center.equalToSuperview()
  170. }
  171. contentView.addSubview(titleLabel)
  172. titleLabel.snp.makeConstraints { make in
  173. make.centerX.equalToSuperview()
  174. make.top.equalTo(24.rpx)
  175. }
  176. contentView.addSubview(addInputView)
  177. addInputView.snp.makeConstraints { make in
  178. make.left.equalTo(29.rpx)
  179. make.right.equalTo(-19.rpx)
  180. make.top.equalTo(titleLabel.snp.bottom).offset(16.rpx)
  181. make.height.equalTo(44.rpx)
  182. }
  183. addInputView.addSubview(addressBookBtn)
  184. addressBookBtn.snp.makeConstraints { make in
  185. make.width.equalTo(104.rpx)
  186. make.top.bottom.right.equalTo(0)
  187. }
  188. addInputView.addSubview(lineView)
  189. lineView.snp.makeConstraints { make in
  190. make.top.equalTo(16.rpx)
  191. make.bottom.equalTo(-16.rpx)
  192. make.right.equalTo(addressBookBtn.snp.left)
  193. make.width.equalTo(1.rpx)
  194. }
  195. addInputView.addSubview(phoneTextField)
  196. phoneTextField.snp.makeConstraints { make in
  197. make.left.equalTo(12.rpx)
  198. make.top.bottom.equalTo(0)
  199. make.right.equalTo(lineView.snp.left).offset(-12.rpx)
  200. }
  201. contentView.addSubview(firstButton)
  202. firstButton.snp.makeConstraints { make in
  203. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  204. make.left.equalTo(24.rpx)
  205. make.bottom.equalTo(-24.rpx)
  206. }
  207. contentView.addSubview(secondButton)
  208. secondButton.snp.makeConstraints { make in
  209. make.size.equalTo(CGSize(width: 118.rpx, height: 40.rpx))
  210. make.right.equalTo(-24.rpx)
  211. make.bottom.equalTo(-24.rpx)
  212. }
  213. contentView.addSubview(closeButton)
  214. closeButton.snp.makeConstraints { make in
  215. make.size.equalTo(CGSize(width: 24.rpx, height: 24.rpx))
  216. make.top.equalTo(12.rpx)
  217. make.right.equalTo(-12.rpx)
  218. }
  219. }
  220. }