Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
17
#import "FirebaseMessaging/Sources/Token/FIRMessagingBackupExcludedPlist.h"
18
 
19
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
20
 
21
@interface FIRMessagingBackupExcludedPlist ()
22
 
23
@property(nonatomic, readwrite, copy) NSString *fileName;
24
@property(nonatomic, readwrite, copy) NSString *subDirectoryName;
25
@property(nonatomic, readwrite, strong) NSDictionary *cachedPlistContents;
26
 
27
@end
28
 
29
@implementation FIRMessagingBackupExcludedPlist
30
 
31
- (instancetype)initWithFileName:(NSString *)fileName subDirectory:(NSString *)subDirectory {
32
  self = [super init];
33
  if (self) {
34
    _fileName = [fileName copy];
35
    _subDirectoryName = [subDirectory copy];
36
  }
37
  return self;
38
}
39
 
40
- (BOOL)writeDictionary:(NSDictionary *)dict error:(NSError **)error {
41
  NSString *path = [self plistPathInDirectory];
42
  if (![dict writeToFile:path atomically:YES]) {
43
    FIRMessagingLoggerError(kFIRMessagingMessageCodeBackupExcludedPlist000,
44
                            @"Failed to write to %@.plist", self.fileName);
45
    return NO;
46
  }
47
 
48
  // Successfully wrote contents -- change the in-memory contents
49
  self.cachedPlistContents = [dict copy];
50
 
51
  NSURL *URL = [NSURL fileURLWithPath:path];
52
  if (error) {
53
    *error = nil;
54
  }
55
 
56
  NSDictionary *preferences = [URL resourceValuesForKeys:@[ NSURLIsExcludedFromBackupKey ]
57
                                                   error:error];
58
  if ([preferences[NSURLIsExcludedFromBackupKey] boolValue]) {
59
    return YES;
60
  }
61
 
62
  BOOL success = [URL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:error];
63
  if (!success) {
64
    FIRMessagingLoggerError(kFIRMessagingMessageCodeBackupExcludedPlist001,
65
                            @"Error excluding %@ from backup, %@", [URL lastPathComponent],
66
                            error ? *error : @"");
67
  }
68
  return success;
69
}
70
 
71
- (BOOL)deleteFile:(NSError **)error {
72
  BOOL success = YES;
73
  NSString *path = [self plistPathInDirectory];
74
  if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
75
    success = [[NSFileManager defaultManager] removeItemAtPath:path error:error];
76
  }
77
  // remove the in-memory contents
78
  self.cachedPlistContents = nil;
79
  return success;
80
}
81
 
82
- (NSDictionary *)contentAsDictionary {
83
  if (!self.cachedPlistContents) {
84
    NSString *path = [self plistPathInDirectory];
85
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
86
      self.cachedPlistContents = [[NSDictionary alloc] initWithContentsOfFile:path];
87
    }
88
  }
89
  return self.cachedPlistContents;
90
}
91
 
92
- (BOOL)doesFileExist {
93
  NSString *path = [self plistPathInDirectory];
94
  return [[NSFileManager defaultManager] fileExistsAtPath:path];
95
}
96
 
97
#pragma mark - Private
98
 
99
- (NSString *)plistPathInDirectory {
100
  NSArray *directoryPaths;
101
  NSString *plistNameWithExtension = [NSString stringWithFormat:@"%@.plist", self.fileName];
102
  directoryPaths =
103
      NSSearchPathForDirectoriesInDomains([self supportedDirectory], NSUserDomainMask, YES);
104
  NSArray *components = @[ directoryPaths.lastObject, _subDirectoryName, plistNameWithExtension ];
105
 
106
  return [NSString pathWithComponents:components];
107
}
108
 
109
- (NSSearchPathDirectory)supportedDirectory {
110
#if TARGET_OS_TV
111
  return NSCachesDirectory;
112
#else
113
  return NSApplicationSupportDirectory;
114
#endif
115
}
116
 
117
@end