UserDefaultsUtils.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /// 是否第一次登录
  11. case isOpened
  12. /// 上次登录的手机号
  13. case loginPhone
  14. /// 已检索商品信息
  15. case reSearchGoods
  16. /// 是否显示风险提示
  17. case isShowRisk
  18. /// 地址信息
  19. case addressInfo
  20. /// 是否记住密码
  21. case isSavePwd
  22. /// 保存密码
  23. case password
  24. /// 保存密码时间戳
  25. case passwordTime
  26. /// 商品最后更新时间
  27. case lastUpdateTime
  28. /// 字体大小
  29. case fontSize
  30. /// 密码有效期
  31. case pwdDown
  32. }
  33. /// UserDefault相关工具类
  34. class UserDefaultsUtils {
  35. // MARK: - 生命周期相关
  36. static func isFirstOpen() -> Bool {
  37. if let isOpened = UserDefaults.standard.value(forKey: UserDefaultsType.isOpened.rawValue) as? Bool {
  38. return !isOpened
  39. } else {
  40. return true
  41. }
  42. }
  43. static func setOpened() {
  44. UserDefaults.standard.set(true, forKey: UserDefaultsType.isOpened.rawValue)
  45. }
  46. /// 获取是否记住密码
  47. /// - Returns: Bool
  48. static func isSavePwd() -> Bool {
  49. if let isSavePwd = UserDefaults.standard.value(forKey: UserDefaultsType.isSavePwd.rawValue) as? Bool {
  50. return isSavePwd
  51. } else {
  52. return false
  53. }
  54. }
  55. /// 设置是否记住密码
  56. /// - Parameter save: save
  57. static func setIsSavePwd(_ save: Bool) {
  58. UserDefaults.standard.set(save, forKey: UserDefaultsType.isSavePwd.rawValue)
  59. }
  60. /// 获取记录的密码
  61. /// - Returns: Bool
  62. static func savePwd() -> String? {
  63. if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.password.rawValue) as? String {
  64. return pwd
  65. } else {
  66. return nil
  67. }
  68. }
  69. /// 设置记住密码
  70. /// - Parameter save: save
  71. static func setSavePwd(_ pwd: String) {
  72. UserDefaults.standard.set(pwd, forKey: UserDefaultsType.password.rawValue)
  73. }
  74. /// 获取记录的密码时间戳
  75. /// - Returns: Bool
  76. static func savePwdTime() -> Date? {
  77. if let pwd = UserDefaults.standard.value(forKey: UserDefaultsType.passwordTime.rawValue) as? Date {
  78. return pwd
  79. } else {
  80. return nil
  81. }
  82. }
  83. /// 设置记住密码时间戳
  84. /// - Parameter save: save
  85. static func setSavePwdTime(_ time: Date) {
  86. UserDefaults.standard.set(time, forKey: UserDefaultsType.passwordTime.rawValue)
  87. }
  88. /// 获取地址信息
  89. /// - Returns: Bool
  90. static func addressInfo() -> MoAddress? {
  91. if let jsonString = UserDefaults.standard.value(forKey: UserDefaultsType.addressInfo.rawValue) as? String,
  92. let address = MoAddress.deserialize(from: jsonString) {
  93. return address
  94. } else {
  95. return nil
  96. }
  97. }
  98. /// 存储地址信息
  99. /// - Parameter address: address
  100. static func setAddressInfo(_ address: MoAddress) {
  101. UserDefaults.standard.set(address.toJSONString() ?? "", forKey: UserDefaultsType.addressInfo.rawValue)
  102. }
  103. /// 是否同意用户隐私协议
  104. /// - Returns: Bool
  105. static func isPrivacy() -> Bool {
  106. if let isShowRisk = UserDefaults.standard.value(forKey: UserDefaultsType.isShowRisk.rawValue) as? Bool {
  107. return isShowRisk
  108. } else {
  109. return false
  110. }
  111. }
  112. /// 设置是否同意用户隐私协议
  113. static func setShowIsPrivacy(_ isPrivacy: Bool) {
  114. UserDefaults.standard.set(isPrivacy, forKey: UserDefaultsType.isShowRisk.rawValue)
  115. }
  116. /// 字体大小
  117. /// - Returns: Bool
  118. static func fontSize() -> CGFloat {
  119. if let size = UserDefaults.standard.value(forKey: UserDefaultsType.fontSize.rawValue) as? CGFloat {
  120. return size
  121. } else {
  122. return 1.0
  123. }
  124. }
  125. /// 系统设置字体大小
  126. static func setFontSize(_ size: CGFloat) {
  127. UserDefaults.standard.set(size, forKey: UserDefaultsType.fontSize.rawValue)
  128. }
  129. /// 获取上次登录的手机号码
  130. ///
  131. /// - Returns: String
  132. static func getLoginPhone() -> String? {
  133. return UserDefaults.standard.value(forKey: UserDefaultsType.loginPhone.rawValue) as? String
  134. }
  135. /// 设置登录手机号信息
  136. ///
  137. /// - Parameter phone: 手机号码
  138. static func setLoginPhone(_ phone: String) {
  139. UserDefaults.standard.setValue(phone, forKey: UserDefaultsType.loginPhone.rawValue)
  140. UserDefaults.standard.synchronize()
  141. }
  142. /// 设置已搜索的商品数据信息
  143. /// - Parameter result: goodsId
  144. static func setResearchGoodsInfo(_ result: String) {
  145. var results = UserDefaultsUtils.getResearchGoodsInfo()
  146. if !results.contains(result) { results.append(result) }
  147. UserDefaults.standard.setValue(results, forKey: UserDefaultsType.reSearchGoods.rawValue)
  148. UserDefaults.standard.synchronize()
  149. }
  150. /// 获取之前搜索的商品数据信息
  151. static func getResearchGoodsInfo() -> [String] {
  152. return (UserDefaults.standard.value(forKey: UserDefaultsType.reSearchGoods.rawValue) as? [String]) ?? []
  153. }
  154. /// 删除所有的搜索记录
  155. static func deleteAllResearchGoodsInfo() {
  156. UserDefaults.standard.setValue([], forKey: UserDefaultsType.reSearchGoods.rawValue)
  157. UserDefaults.standard.synchronize()
  158. }
  159. /// 登录密码有效期
  160. /// - Returns: Bool
  161. static func pwdDown() -> Int {
  162. if let hour = UserDefaults.standard.value(forKey: UserDefaultsType.pwdDown.rawValue) as? Int {
  163. return hour
  164. } else {
  165. return 1
  166. }
  167. }
  168. /// 登录密码有效期
  169. static func setPwdDown(_ hour: Int) {
  170. UserDefaults.standard.set(hour, forKey: UserDefaultsType.pwdDown.rawValue)
  171. }
  172. }
  173. protocol UserDefaultsSettable {
  174. static var name: String { get }
  175. associatedtype defaultKeys: RawRepresentable
  176. }
  177. extension UserDefaultsSettable where defaultKeys.RawValue == String {
  178. static func set(value: String?, forKey key: defaultKeys) {
  179. let aKey = name + key.rawValue
  180. UserDefaults.standard.set(value, forKey: aKey)
  181. }
  182. static func string(forKey key: defaultKeys) -> String? {
  183. let aKey = name + key.rawValue
  184. return UserDefaults.standard.string(forKey: aKey)
  185. }
  186. }
  187. extension UserDefaults {
  188. /// 商品更新时间戳
  189. struct goodsUpdateTime: UserDefaultsSettable {
  190. static var name: String = "goodsUpdateTime"
  191. enum defaultKeys: String {
  192. case actual
  193. }
  194. }
  195. /// 错误码更新时间戳
  196. struct errorcodeUTime: UserDefaultsSettable {
  197. static var name: String = "errorcodeUTime"
  198. enum defaultKeys: String {
  199. case actual
  200. }
  201. }
  202. /// 枚举常量
  203. struct enumUTime: UserDefaultsSettable {
  204. static var name: String = "enumUTime"
  205. enum defaultKeys: String {
  206. case actual
  207. }
  208. }
  209. }