You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.4 KiB

6 years ago
  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 "FIRAuthUserDefaultsStorage.h"
  17. #if FIRAUTH_USER_DEFAULTS_STORAGE_AVAILABLE
  18. NS_ASSUME_NONNULL_BEGIN
  19. static NSString *const kPersistentDomainNamePrefix = @"com.google.Firebase.Auth.";
  20. @implementation FIRAuthUserDefaultsStorage {
  21. /** @var _persistentDomainName
  22. @brief The name of the persistent domain in user defaults.
  23. */
  24. NSString *_persistentDomainName;
  25. /** @var _storage
  26. @brief The backing NSUserDefaults storage for this instance.
  27. */
  28. NSUserDefaults *_storage;
  29. }
  30. - (id<FIRAuthStorage>)initWithService:(NSString *)service {
  31. self = [super init];
  32. if (self) {
  33. _persistentDomainName = [kPersistentDomainNamePrefix stringByAppendingString:service];
  34. _storage = [[NSUserDefaults alloc] init];
  35. }
  36. return self;
  37. }
  38. - (nullable NSData *)dataForKey:(NSString *)key error:(NSError **_Nullable)error {
  39. if (error) {
  40. *error = nil;
  41. }
  42. NSDictionary<NSString *, id> *allData = [_storage persistentDomainForName:_persistentDomainName];
  43. return allData[key];
  44. }
  45. - (BOOL)setData:(NSData *)data forKey:(NSString *)key error:(NSError **_Nullable)error {
  46. NSMutableDictionary<NSString *, id> *allData =
  47. [([_storage persistentDomainForName:_persistentDomainName] ?: @{}) mutableCopy];
  48. allData[key] = data;
  49. [_storage setPersistentDomain:allData forName:_persistentDomainName];
  50. return YES;
  51. }
  52. - (BOOL)removeDataForKey:(NSString *)key error:(NSError **_Nullable)error {
  53. NSMutableDictionary<NSString *, id> *allData =
  54. [[_storage persistentDomainForName:_persistentDomainName] mutableCopy];
  55. [allData removeObjectForKey:key];
  56. [_storage setPersistentDomain:allData forName:_persistentDomainName];
  57. return YES;
  58. }
  59. - (void)clear {
  60. [_storage setPersistentDomain:@{} forName:_persistentDomainName];
  61. }
  62. @end
  63. NS_ASSUME_NONNULL_END
  64. #endif // FIRAUTH_USER_DEFAULTS_STORAGE_AVAILABLE