暂无描述

FIRAuthUserDefaults.m 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRAuthUserDefaults.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. static NSString *const kPersistentDomainNamePrefix = @"com.google.Firebase.Auth.";
  19. @implementation FIRAuthUserDefaults {
  20. /** @var _persistentDomainName
  21. @brief The name of the persistent domain in user defaults.
  22. */
  23. NSString *_persistentDomainName;
  24. /** @var _storage
  25. @brief The backing NSUserDefaults storage for this instance.
  26. */
  27. NSUserDefaults *_storage;
  28. }
  29. - (instancetype)initWithService:(NSString *)service {
  30. self = [super init];
  31. if (self) {
  32. _persistentDomainName = [kPersistentDomainNamePrefix stringByAppendingString:service];
  33. _storage = [[NSUserDefaults alloc] init];
  34. }
  35. return self;
  36. }
  37. - (nullable NSData *)dataForKey:(NSString *)key error:(NSError **_Nullable)error {
  38. if (error) {
  39. *error = nil;
  40. }
  41. NSDictionary<NSString *, id> *allData = [_storage persistentDomainForName:_persistentDomainName];
  42. return allData[key];
  43. }
  44. - (BOOL)setData:(NSData *)data forKey:(NSString *)key error:(NSError **_Nullable)error {
  45. NSMutableDictionary<NSString *, id> *allData =
  46. [([_storage persistentDomainForName:_persistentDomainName] ?: @{}) mutableCopy];
  47. allData[key] = data;
  48. [_storage setPersistentDomain:allData forName:_persistentDomainName];
  49. return YES;
  50. }
  51. - (BOOL)removeDataForKey:(NSString *)key error:(NSError **_Nullable)error {
  52. NSMutableDictionary<NSString *, id> *allData =
  53. [[_storage persistentDomainForName:_persistentDomainName] mutableCopy];
  54. [allData removeObjectForKey:key];
  55. [_storage setPersistentDomain:allData forName:_persistentDomainName];
  56. return YES;
  57. }
  58. - (void)clear {
  59. [_storage setPersistentDomain:@{} forName:_persistentDomainName];
  60. }
  61. @end
  62. NS_ASSUME_NONNULL_END