ios8file-sharingios8-extension

iOS8: Sharing common files between apps


Two iOS: AppA and AppB

Both Apps are created by me and I would like to share one single file between both of the apps.

i.e. AppA launches on deviceA and the User saves data on fileA. Later the User launches AppB on the same (deviceA) and also saves data on fileA. Both apps are saving data on the same file.

I'm aware that I can use NSUserDefaults and share Keychain between apps, but that's not what I'm looking for.

I read up on document extensions provider and app groups, but I'm confused if I can use these for this scenario? Or is there any other way to accomplish this?


Solution

  • You can do it using Application Group Container Directory:

    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *appGroupName = @"Z123456789.com.example.app-group"; /* For example */
    
    NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
    NSError* theError = nil;
    if (![fm createDirectoryAtURL: groupContainerURL withIntermediateDirectories:YES attributes:nil error:&theError]) {
        // Handle the error.
    }