Skip to content

Commit b0e838b

Browse files
milos1290hrishileanplum
authored andcommitted
E2-1227 Excluding database from iCloud/iTunes sync (#325)
* E2-1227 Excluding database from iCloud/iTunes sync * Adding test case * Adding test case, fixing formatting
1 parent 05b8ba6 commit b0e838b

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

Example/Tests/Classes/LPFileManagerTest.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,26 @@ - (void)test_nullability
241241
XCTAssertNil(result);
242242
}
243243

244+
- (void)test_addSkipBackupAttribute
245+
{
246+
NSString *path = nil;
247+
XCTAssertFalse([LPFileManager addSkipBackupAttributeToItemAtPath:path]);
248+
249+
// Creates a sample file in directory for test.
250+
NSString *cacheDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
251+
NSUserDomainMask, YES)[0];
252+
253+
NSData *test_data = [@"sample data" dataUsingEncoding:NSUTF8StringEncoding];
254+
path = [cacheDirectory stringByAppendingPathComponent:@"sample.txt"];
255+
[test_data writeToFile:path atomically:YES];
256+
257+
XCTAssertTrue([LPFileManager addSkipBackupAttributeToItemAtPath:path]);
258+
259+
NSURL* url = [NSURL fileURLWithPath:path];
260+
NSNumber* excluded = [NSNumber new];
261+
[url getResourceValue:&excluded forKey:NSURLIsExcludedFromBackupKey error:nil];
262+
263+
XCTAssertEqual([excluded boolValue], YES);
264+
}
265+
244266
@end

Leanplum-SDK/Classes/Managers/LPFileManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,9 @@
7777
current:(NSString *__nonnull)relativeDir
7878
files:(NSMutableArray *__nonnull)files;
7979

80+
/**
81+
* Adds an attribute to a file at filepath to exclude it from iCloud and iTunes backup.
82+
*/
83+
+ (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *__nonnull) filePathString;
84+
8085
@end

Leanplum-SDK/Classes/Managers/LPFileManager.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,4 +834,26 @@ + (void)enableResourceSyncingWithInclusions:(NSArray *)inclusions
834834
}
835835
}
836836

837+
+ (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
838+
{
839+
LP_TRY
840+
if (!filePathString) {
841+
return NO;
842+
}
843+
844+
NSURL* url= [NSURL fileURLWithPath:filePathString];
845+
if (url && [[NSFileManager defaultManager] fileExistsAtPath:[url path]]) {
846+
NSError *error = nil;
847+
BOOL success = [url setResourceValue:[NSNumber numberWithBool: YES]
848+
forKey:NSURLIsExcludedFromBackupKey
849+
error:&error];
850+
if (!success) {
851+
NSLog(@"Leanplum: Error excluding %@ from backup %@", [url lastPathComponent], error);
852+
}
853+
return success;
854+
}
855+
return NO;
856+
LP_END_TRY
857+
}
858+
837859
@end

Leanplum-SDK/Classes/Utilities/LPDatabase.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ - (sqlite3 *)initSQLite
5555
[self handleSQLiteError:@"SQLite fail to open" errorResult:result query:nil];
5656
return nil;
5757
}
58+
59+
if (result == SQLITE_OK) {
60+
if ([LPFileManager addSkipBackupAttributeToItemAtPath:[LPDatabase sqliteFilePath]]) {
61+
NSLog(@"Leanplum: Successfully excluded database from syncing.");
62+
} else {
63+
NSLog(@"Leanplum: Unable to exclude database from syncing.");
64+
}
65+
}
66+
5867
retryOnCorrupt = NO;
5968

6069
// Create tables.

0 commit comments

Comments
 (0)