1

i have a question about Keyboard Extension.

Container app has many images.(ex > 100 images and total 100MB) and container app has saved using NSUserDefaults.

like this:

NSMutableArray* arr = [NSMutableArray new];

for(NSInteger i = 0; i < 100; i++)
{
    UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:@"img%ld",i]];
    NSData* data = UIImagePNGRepresentation(image);
    [arr addObject:data]
}

NSUserDefaults* ud = [[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];
[ud setObject:arr forKey:@"image_data"];
[ud synchronize];

and keyboard app gets image data from NSUserDefaults.

Is it right way that container app send heavy size data to keyboard app?

In special case, when NSUserDefaults instance accesses method(objectForKey:) in keyboard app, keyboard app crashed.

The special case is :

if image data size in NSUserDefaults is bigger than about 30MB(not sure), keyboard app crashed and if the size is smaller than about 30MB, it works well.

Is there max size limit of NSUserDefaults?

i just want like this app.

i want to know that i control heavy size data in keyboard app.

Amanpreet
  • 1,301
  • 3
  • 12
  • 29
dh0rmfpdlxm
  • 301
  • 1
  • 3
  • 16

1 Answers1

1

1) First you have to write image in Shared document directory.

- (NSString *)getSharedLocationPath:(NSString *)appGroupName {
    NSURL *groupContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.yourGroupName"];
    return [groupContainerURL relativePath];
}

2) Write Image to above shared Directory.

    NSString *path = [[self getSharedLocationPath:@"group.yourGroupName"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",images]];
   //Here fileData is your Image data.
   [fileData writeToFile:path atomically:YES];

3) Exactly get this path to Keyboard Extension class same as above.

- (NSString *)getSharedLocationPath:(NSString *)appGroupName {
    NSURL *groupContainerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.yourGroupName"];
    return [groupContainerURL relativePath];
}
//Here get content of directory from shared document directory. 
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self getSharedLocationPath:@"group.yourGroupName"] error:NULL];
    for (Count = 0; Count < (int)[directoryContent count]; Count++)
    {
        NSLog(@"File %d: %@", (Count + 1), [directoryContent objectAtIndex:Count]);
    }
//Now you get your images from app shared directory.
amit_donga
  • 224
  • 1
  • 9