1. 同步sqlit db到iCloud或者從iCloud同步到本地。
if ([strTitle isEqualToString:NSLocalizedString(@"Backup_data_Sync_to_iCloud_Drive", nil)]) {
NSLog(@"同步到云盤");
// TODO: 需要判斷是否有網(wǎng)絡(luò)
// 詢問是否要全部刪除
NSString *strMsg = [NSString stringWithFormat:NSLocalizedString(@"注意: 同步到iCloud操作, 會(huì)覆蓋已在iCloud的備份!", nil)];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:strMsg message:nil preferredStyle:UIAlertControllerStyleAlert];
__weak __typeof(self) weakSelf = self;
UIAlertAction* btn1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"開始同步", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"開始同步 同步到云盤");
if (![self DetermineICloudPermissions]) {
return;
}
[SVProgressHUD show];
NSString *path = [[DataBase sharedDataBase] getOrCreateDBPath];
[LZiCloud uploadToiCloud:path resultBlock:^(NSError *error) {
[SVProgressHUD dismiss];
if (error == nil) {
[SVProgressHUD showInfoWithStatus:@"同步成功"];
} else {
[SVProgressHUD showErrorWithStatus:@"同步出錯(cuò)"];
}
}];
}];
UIAlertAction* btn2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Do_Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
[btn1 setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
[btn2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
[alert addAction : btn1];
[alert addAction : btn2];
[[CommonTool jsd_findVisibleViewController] presentViewController:alert animated:YES completion:nil];
}else if ([strTitle isEqualToString:NSLocalizedString(@"Sync_data_from_iCloud_Drive_to_local", nil)]) {
NSLog(@"云盤同步到本地");
// 詢問
NSString *strMsg = [NSString stringWithFormat:NSLocalizedString(@"注意: 從iCloud同步到本地操作, 會(huì)覆蓋本地已有的數(shù)據(jù)!", nil)];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:strMsg message:nil preferredStyle:UIAlertControllerStyleAlert];
__weak __typeof(self) weakSelf = self;
UIAlertAction* btn1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"開始同步", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"開始同步 云盤同步到本地");
if (![self DetermineICloudPermissions]) {
return;
}
[SVProgressHUD show];
[LZiCloud downloadFromiCloudWithBlock:^(id obj) {
if (obj != nil) {
NSData *data = (NSData *)obj;
// NSString *strDataBasePath = [LZSqliteTool LZCreateSqliteWithName:LZSqliteName];
NSString *strDataBasePath = [[DataBase sharedDataBase] getOrCreateDBPath];
[data writeToFile:strDataBasePath atomically:YES];
[SVProgressHUD dismiss];
[SVProgressHUD showInfoWithStatus:@"同步成功"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Reset_APP_RootViewController_Notify" object:@"needJumpIndex1"];
} else {
[SVProgressHUD dismiss];
[SVProgressHUD showErrorWithStatus:@"同步出錯(cuò)"];
}
}];
}];
UIAlertAction* btn2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Do_Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
[btn1 setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
[btn2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
[alert addAction : btn1];
[alert addAction : btn2];
[[CommonTool jsd_findVisibleViewController] presentViewController:alert animated:YES completion:nil];
}
2. 修改DB文件
- (NSString*)getOrCreateDBPath;
- (NSString*)getOrCreateDBPath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [path objectAtIndex:0];
NSString *dstPath = [docDirectory stringByAppendingPathComponent:@"MakeALittleProgressEveryDay.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dstPath])
{
NSLog(@"文件不存在");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MakeALittleProgressEveryDay" ofType:@"db"];
NSLog(@"filePath: %@",filePath);
NSLog(@"dstPath: %@",dstPath);
[[NSFileManager defaultManager] copyItemAtPath:filePath toPath:dstPath error:nil];
}else{
NSLog(@"文件存在 %@",dstPath);
}
return dstPath;
}
3. 修改PCH文件
// iCloud同步
//數(shù)據(jù)庫表格
#define LZSqliteName @"userData"
#define LZSqliteDataTableName @"newUserAccountData"
#define LZSqliteGroupTableName @"userAccountGroup"
#define LZSqliteDataPasswordKey @"passwordKey"
//數(shù)據(jù)庫數(shù)據(jù)有更新的通知key
#define LZSqliteValuesChangedKey @"sqliteValuesChanged"
4. 把LZICloudTools目錄文件,添加到工程中

Snip20221015_4.png
參考:makeProgress