Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2019 Google
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULSecureCoding.h"
16
 
17
NSString *const kGULSecureCodingError = @"GULSecureCodingError";
18
 
19
@implementation GULSecureCoding
20
 
21
+ (nullable id)unarchivedObjectOfClasses:(NSSet<Class> *)classes
22
                                fromData:(NSData *)data
23
                                   error:(NSError **)outError {
24
  id object;
25
#if __has_builtin(__builtin_available)
26
  if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)) {
27
    object = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:data error:outError];
28
  } else
29
#endif  // __has_builtin(__builtin_available)
30
  {
31
    @try {
32
#pragma clang diagnostic push
33
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
34
      NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
35
#pragma clang diagnostic pop
36
      unarchiver.requiresSecureCoding = YES;
37
 
38
      object = [unarchiver decodeObjectOfClasses:classes forKey:NSKeyedArchiveRootObjectKey];
39
    } @catch (NSException *exception) {
40
      if (outError) {
41
        *outError = [self archivingErrorWithException:exception];
42
      }
43
    }
44
 
45
    if (object == nil && outError && *outError == nil) {
46
      NSString *failureReason = @"NSKeyedUnarchiver failed to unarchive data.";
47
      *outError = [NSError errorWithDomain:kGULSecureCodingError
48
                                      code:-1
49
                                  userInfo:@{NSLocalizedFailureReasonErrorKey : failureReason}];
50
    }
51
  }
52
 
53
  return object;
54
}
55
 
56
+ (nullable id)unarchivedObjectOfClass:(Class)class
57
                              fromData:(NSData *)data
58
                                 error:(NSError **)outError {
59
  return [self unarchivedObjectOfClasses:[NSSet setWithObject:class] fromData:data error:outError];
60
}
61
 
62
+ (nullable NSData *)archivedDataWithRootObject:(id<NSCoding>)object error:(NSError **)outError {
63
  NSData *archiveData;
64
#if __has_builtin(__builtin_available)
65
  if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)) {
66
    archiveData = [NSKeyedArchiver archivedDataWithRootObject:object
67
                                        requiringSecureCoding:YES
68
                                                        error:outError];
69
  } else
70
#endif  // __has_builtin(__builtin_available)
71
  {
72
    @try {
73
      NSMutableData *data = [NSMutableData data];
74
#pragma clang diagnostic push
75
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
76
      NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
77
#pragma clang diagnostic pop
78
      archiver.requiresSecureCoding = YES;
79
 
80
      [archiver encodeObject:object forKey:NSKeyedArchiveRootObjectKey];
81
      [archiver finishEncoding];
82
 
83
      archiveData = [data copy];
84
    } @catch (NSException *exception) {
85
      if (outError) {
86
        *outError = [self archivingErrorWithException:exception];
87
      }
88
    }
89
  }
90
 
91
  return archiveData;
92
}
93
 
94
+ (NSError *)archivingErrorWithException:(NSException *)exception {
95
  NSString *failureReason = [NSString
96
      stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
97
                       exception.name, exception.reason, exception.userInfo];
98
  NSDictionary *errorUserInfo = @{NSLocalizedFailureReasonErrorKey : failureReason};
99
 
100
  return [NSError errorWithDomain:kGULSecureCodingError code:-1 userInfo:errorUserInfo];
101
}
102
 
103
@end