Work around limitation of NSUserDefault 4Mb Size

This commit is contained in:
Christophe Deschamps 2020-02-18 22:21:02 +01:00 committed by Danmei Chen
parent 0029f580aa
commit d1614e271e
2 changed files with 55 additions and 12 deletions

View file

@ -297,6 +297,25 @@ static UICompositeViewDescription *compositeDescription = nil;
}
}
-(void) nsDataWrite:(NSData *)data {
NSString* groupName = [NSString stringWithFormat:@"group.%@.linphoneExtension",[[NSBundle mainBundle] bundleIdentifier]];
NSError *error = nil;
NSString *path =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupName] path];
NSString *fullCacheFilePathPath = [NSString stringWithFormat:@"%@/%@",path,@"nsData"];
[[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:fullCacheFilePathPath] error:&error];
if (![data writeToFile:fullCacheFilePathPath atomically:YES]) {
NSLog(@"nsDataWrite error %@",error);
}
}
-(NSData *) nsDataRead {
NSString* groupName = [NSString stringWithFormat:@"group.%@.linphoneExtension",[[NSBundle mainBundle] bundleIdentifier]];
NSString *path =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupName] path];
NSString *fullCacheFilePathPath = [NSString stringWithFormat:@"%@/%@",path,@"nsData"];
return[NSData dataWithContentsOfFile:fullCacheFilePathPath];
}
- (void)shareFile {
NSString* groupName = [NSString stringWithFormat:@"group.%@.linphoneExtension",[[NSBundle mainBundle] bundleIdentifier]];
@ -313,12 +332,12 @@ static UICompositeViewDescription *compositeDescription = nil;
PHAsset *phasset = [assetDict objectForKey:key];
if (!phasset) {
// for the images or videos not really in the photo album
[self confirmShare:dict[@"nsData"] url:nil fileName:fileName assetId:nil];
[self confirmShare:[self nsDataRead] url:nil fileName:fileName assetId:nil];
} else if ([fileName hasSuffix:@"JPG"] || [fileName hasSuffix:@"PNG"] || [fileName hasSuffix:@"jpg"] || [fileName hasSuffix:@"png"]) {
UIImage *image = [[UIImage alloc] initWithData:dict[@"nsData"]];
UIImage *image = [[UIImage alloc] initWithData:[self nsDataRead]];
[self chooseImageQuality:image assetId:[phasset localIdentifier]];
} else if ([fileName hasSuffix:@"MOV"] || [fileName hasSuffix:@"mov"]) {
[self confirmShare:dict[@"nsData"] url:nil fileName:nil assetId:[phasset localIdentifier]];
[self confirmShare:[self nsDataRead] url:nil fileName:nil assetId:[phasset localIdentifier]];
} else {
LOGE(@"Unable to parse file %@",fileName);
}
@ -327,7 +346,7 @@ static UICompositeViewDescription *compositeDescription = nil;
} else if (dictFile) {
NSString *fileName = dictFile[@"url"];
[_messageField setText:dictFile[@"message"]];
[self confirmShare:dictFile[@"nsData"] url:nil fileName:fileName assetId:nil];
[self confirmShare:[self nsDataRead] url:nil fileName:fileName assetId:nil];
[defaults removeObjectForKey:@"icloudData"];
} else if (dictUrl) {

View file

@ -66,8 +66,32 @@
return @[];
}
- (void)loadItem:(NSItemProvider *)provider typeIdentifier:(NSString *)typeIdentifier defaults:(NSUserDefaults *)defaults
{
- (NSString *)cacheDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
return cachePath;
}
-(void) nsDataWrite:(NSData *)data {
NSString* groupName = [NSString stringWithFormat:@"group.%@",[[NSBundle mainBundle] bundleIdentifier]];
NSError *error = nil;
NSString *path =[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupName] path];
NSString *fullCacheFilePathPath = [NSString stringWithFormat:@"%@/%@",path,@"nsData"];
[[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:fullCacheFilePathPath] error:&error];
if (![data writeToFile:fullCacheFilePathPath atomically:YES]) {
NSLog(@"nsDataWrite error");
}
}
- (void)loadItem:(NSItemProvider *)provider typeIdentifier:(NSString *)typeIdentifier defaults:(NSUserDefaults *)defaults {
[provider loadItemForTypeIdentifier:typeIdentifier options:nil completionHandler:^(id<NSSecureCoding> _Nullable item, NSError * _Null_unspecified error) {
if([(NSObject*)item isKindOfClass:[NSURL class]]) {
NSURL *url = (NSURL *)item;
@ -78,15 +102,15 @@
NSString *filename = [imgPath lastPathComponent];
if([imgPath containsString:@"var/mobile/Media/PhotoData"]) {
// We get the corresponding PHAsset identifier so we can display the image in the app without having to duplicate it.
NSDictionary *dict = @{@"nsData" : nsData,
@"url" : filename,
NSDictionary *dict = @{@"url" : filename,
@"message" : self.contentText};
[self nsDataWrite:nsData];
[defaults setObject:dict forKey:@"photoData"];
} else if ([imgPath containsString:@"var/mobile/Library/Mobile Documents/com~apple~CloudDocs"] || [[url scheme] isEqualToString:@"file"]) {
// shared files from icloud drive
NSDictionary *dict = @{@"nsData" : nsData,
@"url" : filename,
NSDictionary *dict = @{@"url" : filename,
@"message" : self.contentText};
[self nsDataWrite:nsData];
[defaults setObject:dict forKey:@"icloudData"];
} else {
NSDictionary *dict = @{@"url" : [url absoluteString],
@ -103,9 +127,9 @@
[self respondUrl:defaults];
} else if ([(NSObject*)item isKindOfClass:[UIImage class]]) {
UIImage *image = (UIImage*)item;
NSDictionary *dict = @{@"nsData" : UIImagePNGRepresentation(image),
@"url" : [NSString stringWithFormat:@"IMAGE_%f.PNG", [[NSDate date] timeIntervalSince1970]],
NSDictionary *dict = @{@"url" : [NSString stringWithFormat:@"IMAGE_%f.PNG", [[NSDate date] timeIntervalSince1970]],
@"message" : self.contentText};
[self nsDataWrite:UIImagePNGRepresentation(image)];
[defaults setObject:dict forKey:@"photoData"];
[self respondUrl:defaults];