UserDefaultsUtils.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /// UserDefault相关工具类
  17. class UserDefaultsUtils {
  18. // MARK: - 生命周期相关
  19. static func isFirstOpen() -> Bool {
  20. if let isOpened = UserDefaults.standard.value(forKey: UserDefaultsType.isOpened.rawValue) as? Bool {
  21. return !isOpened
  22. } else {
  23. return true
  24. }
  25. }
  26. static func setOpened() {
  27. UserDefaults.standard.set(true, forKey: UserDefaultsType.isOpened.rawValue)
  28. }
  29. /// 获取地址信息
  30. /// - Returns: Bool
  31. static func addressInfo() -> MoAddress? {
  32. if let jsonString = UserDefaults.standard.value(forKey: UserDefaultsType.addressInfo.rawValue) as? String,
  33. let address = MoAddress.deserialize(from: jsonString) {
  34. return address
  35. } else {
  36. return nil
  37. }
  38. }
  39. /// 存储地址信息
  40. /// - Parameter address: address
  41. static func setAddressInfo(_ address: MoAddress) {
  42. UserDefaults.standard.set(address.toJSONString() ?? "", forKey: UserDefaultsType.addressInfo.rawValue)
  43. }
  44. /// 设置通知未读标志
  45. ///
  46. /// - Parameter ids: ids
  47. static func setUnReasNoticeIds(_ ids: [String]) {
  48. UserDefaults.standard.setValue(ids, forKey: UserDefaultsType.noticeRead.rawValue)
  49. UserDefaults.standard.synchronize()
  50. }
  51. /// 获取未读标志
  52. ///
  53. /// - Returns: [String]
  54. static func getUnReadNoticeIds() -> [String] {
  55. let ids = UserDefaults.standard.value(forKey: UserDefaultsType.noticeRead.rawValue) ?? []
  56. return ids as! [String]
  57. }
  58. /// 获取上次登录的手机号码
  59. ///
  60. /// - Returns: String
  61. static func getLoginPhone() -> String? {
  62. return UserDefaults.standard.value(forKey: UserDefaultsType.loginPhone.rawValue) as? String
  63. }
  64. /// 设置登录手机号信息
  65. ///
  66. /// - Parameter phone: 手机号码
  67. static func setLoginPhone(_ phone: String) {
  68. UserDefaults.standard.setValue(phone, forKey: UserDefaultsType.loginPhone.rawValue)
  69. UserDefaults.standard.synchronize()
  70. }
  71. /// 设置已搜索的商品数据信息
  72. /// - Parameter result: goodsId
  73. static func setResearchGoodsInfo(_ result: String) {
  74. var results = UserDefaultsUtils.getResearchGoodsInfo()
  75. if !results.contains(result) { results.append(result) }
  76. UserDefaults.standard.setValue(results, forKey: UserDefaultsType.reSearchGoods.rawValue)
  77. UserDefaults.standard.synchronize()
  78. }
  79. /// 获取之前搜索的商品数据信息
  80. static func getResearchGoodsInfo() -> [String] {
  81. return (UserDefaults.standard.value(forKey: UserDefaultsType.reSearchGoods.rawValue) as? [String]) ?? []
  82. }
  83. /// 删除所有的搜索记录
  84. static func deleteAllResearchGoodsInfo() {
  85. UserDefaults.standard.setValue([], forKey: UserDefaultsType.reSearchGoods.rawValue)
  86. UserDefaults.standard.synchronize()
  87. }
  88. }
  89. protocol UserDefaultsSettable {
  90. static var name: String { get }
  91. associatedtype defaultKeys: RawRepresentable
  92. }
  93. extension UserDefaultsSettable where defaultKeys.RawValue == String {
  94. static func set(value: String?, forKey key: defaultKeys) {
  95. let aKey = name + key.rawValue
  96. UserDefaults.standard.set(value, forKey: aKey)
  97. }
  98. static func string(forKey key: defaultKeys) -> String? {
  99. let aKey = name + key.rawValue
  100. return UserDefaults.standard.string(forKey: aKey)
  101. }
  102. }
  103. extension UserDefaults {
  104. /// 商品更新时间戳
  105. struct goodsUpdateTime: UserDefaultsSettable {
  106. static var name: String = "goodsUpdateTime"
  107. enum defaultKeys: String {
  108. case actual
  109. }
  110. }
  111. /// 错误码更新时间戳
  112. struct errorcodeUTime: UserDefaultsSettable {
  113. static var name: String = "errorcodeUTime"
  114. enum defaultKeys: String {
  115. case actual
  116. }
  117. }
  118. }