fix: disable backup of mmkv keys in icloud (#5165)

* set accessible key to mmkv

* update patch package
This commit is contained in:
Gleidson Daniel Silva 2023-08-17 16:41:20 -03:00 committed by GitHub
parent f78f6f33ff
commit 96bb25e5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 3 deletions

View File

@ -4,8 +4,10 @@ export class MMKVLoader {
}
setProcessingMode = jest.fn().mockImplementation(() => ({
withEncryption: jest.fn().mockImplementation(() => ({
initialize: jest.fn().mockImplementation(() => {})
setAccessibleIOS: jest.fn().mockImplementation(() => ({
withEncryption: jest.fn().mockImplementation(() => ({
initialize: jest.fn().mockImplementation(() => {})
}))
}))
}));
}
@ -14,4 +16,8 @@ export const ProcessingModes = {
MULTI_PROCESS: ''
};
export const IOSAccessibleStates = {
AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY: ''
};
export const create = jest.fn();

View File

@ -1,8 +1,9 @@
import { create, MMKVLoader, MMKVInstance, ProcessingModes } from 'react-native-mmkv-storage';
import { create, MMKVLoader, MMKVInstance, ProcessingModes, IOSAccessibleStates } from 'react-native-mmkv-storage';
const MMKV = new MMKVLoader()
// MODES.MULTI_PROCESS = ACCESSIBLE BY APP GROUP (iOS)
.setProcessingMode(ProcessingModes.MULTI_PROCESS)
.setAccessibleIOS(IOSAccessibleStates.AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY)
.withEncryption()
.initialize();

View File

@ -1,3 +1,51 @@
diff --git a/node_modules/react-native-mmkv-storage/ios/MMKVNative.mm b/node_modules/react-native-mmkv-storage/ios/MMKVNative.mm
index 9af089d..12ff53b 100644
--- a/node_modules/react-native-mmkv-storage/ios/MMKVNative.mm
+++ b/node_modules/react-native-mmkv-storage/ios/MMKVNative.mm
@@ -37,12 +37,43 @@ - (instancetype)init
rPath = rootDir;
_secureStorage = [[SecureStorage alloc] init];
[MMKV initializeMMKV:rootDir];
+ [self addSkipBackupAttributeToDir:rootDir];
});
return self;
}
+- (BOOL)addSkipBackupAttributeToURLAtPath:(NSURL *)url
+{
+ if (!url) return NO;
+ if (![[NSFileManager defaultManager] fileExistsAtPath:url.path]) return NO;
+
+ NSError *error = nil;
+ NSNumber *value = nil;
+ BOOL success = [url getResourceValue:&value forKey:NSURLIsExcludedFromBackupKey error:&error];
+ if (value.boolValue == YES) {
+ NSLog(@"%@ already marked for backup exclusion", [url lastPathComponent]);
+ return YES;
+ }
+
+ success = [url setResourceValue:[NSNumber numberWithBool:YES]
+ forKey:NSURLIsExcludedFromBackupKey error:&error];
+ if(!success){
+ NSLog(@"Error excluding %@ from backup: %@", [url lastPathComponent], error);
+ } else {
+ NSLog(@"Succesfully marked %@ for backup exclusion", [url lastPathComponent]);
+ }
+ return success;
+}
+
+- (BOOL) addSkipBackupAttributeToDir:(NSString *)path
+{
+ if (!path) return NO;
+ NSURL *pathUrl = [[NSURL alloc] initFileURLWithPath:path isDirectory:YES];
+ return [self addSkipBackupAttributeToURLAtPath:pathUrl];
+}
+
MMKV *getInstance(NSString *ID) {
if ([[mmkvInstances allKeys] containsObject:ID]) {
MMKV *kv = [mmkvInstances objectForKey:ID];
diff --git a/node_modules/react-native-mmkv-storage/ios/SecureStorage.m b/node_modules/react-native-mmkv-storage/ios/SecureStorage.m
index dbea26b..2483375 100644
--- a/node_modules/react-native-mmkv-storage/ios/SecureStorage.m