Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing file/data between two iOS apps that I own

I have apps A and B and both are developed by me. I want to send file from A to B but it has to happen without using any sharing services like dropbox or anything that involves internet connection.

OpenURL/CustomURLSchemes doesn't seem to work since you are limited with the data you can pass and the file size can be big. And because of the sandboxing I can't write the file and pass the url... UIActivityViewController and UIDocumentInteractionController are options only if there is a way to display just one app which I fail to achieve so far..

Is this possible?

I own both apps and the file extension is custom.

like image 360
nihilvex Avatar asked Jul 09 '15 14:07

nihilvex


1 Answers

You actually don't need extensions to share data between your own apps. You can use app groups for this.

In both MyApp1 and MyApp2 goto the target, then capabilities, then click on the app groups capability. Let Xcode help you get app group entitlement setup in your apple developer account.

Now you can use that app group ID to share data between you apps. For instance:

In MyApp1 put this in your appDelegate:

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myappgroup"];
[myDefaults setObject:@"foo" forKey:@"bar"];

And in MyApp2 appDelegate put this:

NSUserDefaults *myDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mycompany.myappgroup"];
NSLog(@"Show me something: %@",[myDefaults objectForKey:@"bar"]);

Make sure that the string you used for the suite name is the exact same as what is under the app group capabilities section in Xcode and also the string in your entitlements plist that was automatically added to your project.

You can also share files using the same idea:

NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:
    @"group.mycompany.myappgroup"];

And for those of you who want to see it in Swift:

var userDefaults = NSUserDefaults(suiteName: "group.mycompany.myappgroup")

var fileManager = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.mycompany.myappgroup")
like image 122
Frankie Avatar answered Sep 24 '22 14:09

Frankie