UserDefaultsUtils.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // UserDefaultsUtils.swift
  3. // MTP2_iOS
  4. //
  5. // Created by zhongyuan on 2018/6/10.
  6. // Copyright © 2018年 zhongyuan.All rights reserved.
  7. //
  8. import Foundation
  9. enum UserDefaultsType: String {
  10. case isOpened
  11. case loginPhone
  12. case noticeRead
  13. case reSearchGoods
  14. case addressInfo
  15. /// 是否记住密码
  16. case isSavePwd
  17. /// 保存密码
  18. case password
  19. /// 保存密码时间戳
  20. case passwordTime
  21. }
  22. /// UserDefault相关工具类
  23. class UserDefaultsUtils {
  24. // MARK: - 生命周期相关
  25. static func isFirstOpen() -> Bool {
  26. if let isOpened = UserDefaults.standard.value(forKey: UserDefaultsType.isOpened.rawValue) as? Bool {
  27. return !isOpened
  28. } else {
  29. return true
  30. }
  31. }
  32. static func setOpened() {
  33. UserDefaults.standard.set(true, forKey: UserDefaultsType.isOpened.rawValue)
  34. }
  35. /// 获取是否记住密码
  36. /// - Returns: Bool
  37. static func isSavePwd() -> Bool {
  38. if let isSavePwd = UserDefaults.standard.value(forKey: UserDefaultsType.isSavePwd.rawValue) as? Bool {
  39. return isSavePwd
  40. } else {
  41. return false
  42. }
  43. }
  44. /// 设置是否记住密码
  45. /// - Parameter save: save
  46. static func setIsSavePwd(_ save: Bool) {
  47. UserDefaults.standard.set(save, forKey: UserDefaultsType.isSavePwd.rawValue)
  48. }
  49. /// 获取记录的密码
  50. /// - Returns: Bool
  51. static func savePwd() -> String? {
  52. if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.password.rawValue) as? String {
  53. return pwd
  54. } else {
  55. return nil
  56. }
  57. }
  58. /// 设置记住密码
  59. /// - Parameter save: save
  60. static func setSavePwd(_ pwd: String) {
  61. UserDefaults.standard.set(pwd, forKey: UserDefaultsType.password.rawValue)
  62. }
  63. /// 获取记录的密码时间戳
  64. /// - Returns: Bool
  65. static func savePwdTime() -> Date? {
  66. if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.passwordTime.rawValue) as? Date {
  67. return pwd
  68. } else {
  69. return nil
  70. }
  71. }
  72. /// 设置记住密码时间戳
  73. /// - Parameter save: save
  74. static func setSavePwdTime(_ time: Date) {
  75. UserDefaults.standard.set(time, forKey: UserDefaultsType.passwordTime.rawValue)
  76. }
  77. /// 获取地址信息
  78. /// - Returns: Bool
  79. static func addressInfo() -> MoAddress? {
  80. if let jsonString = UserDefaults.standard.value(forKey: UserDefaultsType.addressInfo.rawValue) as? String,
  81. let address = MoAddress.deserialize(from: jsonString) {
  82. return address
  83. } else {
  84. return nil
  85. }
  86. }
  87. /// 存储地址信息
  88. /// - Parameter address: address
  89. static func setAddressInfo(_ address: MoAddress) {
  90. UserDefaults.standard.set(address.toJSONString() ?? "", forKey: UserDefaultsType.addressInfo.rawValue)
  91. }
  92. /// 设置通知未读标志
  93. ///
  94. /// - Parameter ids: ids
  95. static func setUnReasNoticeIds(_ ids: [String]) {
  96. UserDefaults.standard.setValue(ids, forKey: UserDefaultsType.noticeRead.rawValue)
  97. UserDefaults.standard.synchronize()
  98. }
  99. /// 获取未读标志
  100. ///
  101. /// - Returns: [String]
  102. static func getUnReadNoticeIds() -> [String] {
  103. let ids = UserDefaults.standard.value(forKey: UserDefaultsType.noticeRead.rawValue) ?? []
  104. return ids as! [String]
  105. }
  106. /// 获取上次登录的手机号码
  107. ///
  108. /// - Returns: String
  109. static func getLoginPhone() -> String? {
  110. return UserDefaults.standard.value(forKey: UserDefaultsType.loginPhone.rawValue) as? String
  111. }
  112. /// 设置登录手机号信息
  113. ///
  114. /// - Parameter phone: 手机号码
  115. static func setLoginPhone(_ phone: String) {
  116. UserDefaults.standard.setValue(phone, forKey: UserDefaultsType.loginPhone.rawValue)
  117. UserDefaults.standard.synchronize()
  118. }
  119. /// 设置已搜索的商品数据信息
  120. /// - Parameter result: goodsId
  121. static func setResearchGoodsInfo(_ result: String) {
  122. var results = UserDefaultsUtils.getResearchGoodsInfo()
  123. if !results.contains(result) { results.append(result) }
  124. UserDefaults.standard.setValue(results, forKey: UserDefaultsType.reSearchGoods.rawValue)
  125. UserDefaults.standard.synchronize()
  126. }
  127. /// 获取之前搜索的商品数据信息
  128. static func getResearchGoodsInfo() -> [String] {
  129. return (UserDefaults.standard.value(forKey: UserDefaultsType.reSearchGoods.rawValue) as? [String]) ?? []
  130. }
  131. /// 删除所有的搜索记录
  132. static func deleteAllResearchGoodsInfo() {
  133. UserDefaults.standard.setValue([], forKey: UserDefaultsType.reSearchGoods.rawValue)
  134. UserDefaults.standard.synchronize()
  135. }
  136. }
  137. protocol UserDefaultsSettable {
  138. static var name: String { get }
  139. associatedtype defaultKeys: RawRepresentable
  140. }
  141. extension UserDefaultsSettable where defaultKeys.RawValue == String {
  142. static func set(value: String?, forKey key: defaultKeys) {
  143. let aKey = name + key.rawValue
  144. UserDefaults.standard.set(value, forKey: aKey)
  145. }
  146. static func string(forKey key: defaultKeys) -> String? {
  147. let aKey = name + key.rawValue
  148. return UserDefaults.standard.string(forKey: aKey)
  149. }
  150. }
  151. extension UserDefaults {
  152. /// 商品更新时间戳
  153. struct goodsUpdateTime: UserDefaultsSettable {
  154. static var name: String = "goodsUpdateTime"
  155. enum defaultKeys: String {
  156. case actual
  157. }
  158. }
  159. /// 错误码更新时间戳
  160. struct errorcodeUTime: UserDefaultsSettable {
  161. static var name: String = "errorcodeUTime"
  162. enum defaultKeys: String {
  163. case actual
  164. }
  165. }
  166. }