Monday, March 2, 2015

iOS Extension Sharing Data between two apps

Solution is to setup an app group. App groups are the scheme iOS uses to share data between two different apps. If the apps have the right entitlements and proper provisioning, they can access a shared directory outside the local sandbox. 

It looks like we can enable the app group from the Application settings in the Xcode. Basically, Xcode is setting up the right entitlements and proper provisioning so that they can access shared directory outside of their normal sandbox. 

When flap the Xcode switch that says App Groups, Xcode contacts to developer centre to configure the app ID for app groups. Next, it will ask the developer for the group name. This will create one group and can download the provisioning profile. IF this doesn’t work from the Xcode, then developers also have a choice to do it from the developer centre online after logging into it. 

The simplest way to use app group is via sharing NSUserDefaults. But it is a little different that the app group is created specific to the domain. 

NSUserDefaults *myAppDefaults = [[NSUserDefaults alloc]initWithSuiteName:@“group.com.mycompany.testapp”];
[myAppDefaults setObject:@“foo” forKey:@“bar”];

the above approach is good if we don’t have to share much data. If apps want to share much data, then NSFileManager can be used as well. like below

NSURL *groupURL =[ [NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@“group.com.mycompany.testApp”];

Any App extension matching the group entitlement can access the same directory. 

References: 

No comments:

Post a Comment