7

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.

nihilvex
  • 221
  • 3
  • 9
  • possible duplicate of [Share data between two or more iPhone applications](http://stackoverflow.com/questions/9425706/share-data-between-two-or-more-iphone-applications) – Vojtech Vrbka Jul 09 '15 at 15:00
  • 1
    [Here is a similar question that might help](http://stackoverflow.com/questions/21048742/how-containerurlforsecurityapplicationgroupidentifier-in-nsfilemanager-works-on) – Alladinian Jul 09 '15 at 15:10

1 Answers1

11

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")
Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Thanks for the quick response! I'll give it a try tomorrow and let you know, but it seems that it will do the job! Cheers! – nihilvex Jul 09 '15 at 16:14
  • Works as a charm! Thanks a lot! For future references Alladinian's comment is also helpful. – nihilvex Jul 10 '15 at 06:19
  • 1
    Can I keep same group name in different developer account to share files between different developers apps? – Sunil Targe Jul 18 '16 at 03:08
  • In Swift 5, it should be like this. let fileManager = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SHARED_GROUP_ID) – Lasantha Basnayake Nov 15 '20 at 10:27