FIRKeyedArchivingUtils.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2019 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 "FirebaseInstallations/Source/Tests/Utils/FIRKeyedArchivingUtils.h"
  17. @implementation FIRKeyedArchivingUtils
  18. + (nullable NSData *)archivedDataWithRootObject:(id)object error:(NSError **)outError {
  19. NSData *archivedData;
  20. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  21. archivedData = [NSKeyedArchiver archivedDataWithRootObject:object
  22. requiringSecureCoding:YES
  23. error:outError];
  24. } else {
  25. @try {
  26. NSMutableData *data = [NSMutableData data];
  27. #pragma clang diagnostic push
  28. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  29. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  30. #pragma clang diagnostic pop
  31. archiver.requiresSecureCoding = YES;
  32. [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
  33. [archiver finishEncoding];
  34. archivedData = [data copy];
  35. } @catch (NSException *exception) {
  36. if (outError) {
  37. NSString *failureReason = [NSString stringWithFormat:@"Exception: %@", exception];
  38. *outError = [NSError errorWithDomain:@"FIRKeyedArchivingUtils"
  39. code:-1
  40. userInfo:@{
  41. NSLocalizedFailureReasonErrorKey : failureReason,
  42. }];
  43. }
  44. }
  45. }
  46. return archivedData;
  47. }
  48. + (nullable id)unarchivedObjectOfClass:(Class)class
  49. fromData:(NSData *)data
  50. error:(NSError **)outError {
  51. id object;
  52. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  53. object = [NSKeyedUnarchiver unarchivedObjectOfClass:class fromData:data error:outError];
  54. } else {
  55. @try {
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  58. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  59. #pragma clang diagnostic pop
  60. unarchiver.requiresSecureCoding = YES;
  61. object = [unarchiver decodeObjectOfClass:class forKey:NSKeyedArchiveRootObjectKey];
  62. } @catch (NSException *exception) {
  63. if (outError) {
  64. NSString *failureReason = [NSString stringWithFormat:@"Exception: %@", exception];
  65. *outError = [NSError errorWithDomain:@"FIRKeyedArchivingUtils"
  66. code:-1
  67. userInfo:@{
  68. NSLocalizedFailureReasonErrorKey : failureReason,
  69. }];
  70. }
  71. }
  72. }
  73. return object;
  74. }
  75. @end