Share data between two or more iPhone applications Share data between two or more iPhone applications xcode xcode

Share data between two or more iPhone applications


In the sandboxed world of iOS development sharing data between applications can prove difficult Since iOS developers can’t share data directly through the file system, they need to find alternate solutions for their applications. Some common solutions include:

  • UIDocumentInteractionController

  • UIActivityViewController

  • Shared Keychain Access

  • Custom URL Scheme

  • Web Service

  • iCloud API


UIDocumentInteractionController:

Allows the user to open a document in any other application that registers as being able to handle a particular document Uniform Type Identifier (UTI).

The UIDocumentInteractionController has been used in the past as a means of opening a document in other applications on the device, for example, opening email attachments from the Mail app.

UIDocumentInteractionController

Unfortunately, the UIDocumentInteractionController‘s UI displays only six applications.

You cannot guarantee that your application will appear in the list. While the UIDocumentInteractionController has not been deprecated, the UIActivityViewController provides a more flexible replacement as of iOS 6.0.

Availability: iOS 3.2+

Pros:

  • Allows sharing of common data types with a wide array ofapplications.

Cons:

  • Allows control of the type of data sent to the UIDocumentInteractionController, but not the destinations.

  • Requires additional user interaction.

  • Limited number of data destinations may cause your application not to display in the list.


UIActivityViewController:

Allows the user to perform a number of actions with an array of data.

For example they may print, email, copy, post to social media, or open in another application.

You may create your own UIActivity subclasses to provide custom services to the user.

UIActivityController

Availability: iOS 6.0+

Pros:

  • Great for sharing common data types with a wide array of applications and social media.

  • Can supply an array of items for application to an activity. Objects should conform to UIActivityItemSource protocol.

  • Has the ability to set excluded activity types.

  • Paging UI allows for more data destinations than UIDocumentInteractionController.

Cons:

  • You must define a custom activity type to restrict “Open In…” destinations of common data types.

  • Requires additional user interaction.


Shared Keychain Access:

Allows you to securely store data to a shared keychain that other applications that are part of a suite of applications can access.

All applications that share keychain access must use the same app ID prefix.

For an example of shared keychain access in action. See Apple’s GenericKeychain sample code.

Shared Key Chain

Availability: iOS 3.0+

Pros:

  • Secure access to data.

Cons:

  • You can only share data between applications that share a common app ID prefix.

  • The Keychain API on the iOS Simulator comes from OS X, which has different API than that of the iOS device.


Custom URL Scheme:

Allows data to pass between applications using simple URLs.

Custom URL Scheme

Availability: iOS 3.0+

Pros:

  • No network connection required.
  • Great for small amounts of data that you can easily encode into an escaped, legal URL.

Cons:

  • You must encode data into an escaped legal URL.

    Note: base64 encoding has seen common use turning serializable data into a string value. However, base64 strings may include characters that are invalid for use in URLs. You might consider using base64url. See Base 64 Encoding with URL and Filename Safe Alphabet for more information.


iCloud API:

Everybody knows about what is iCloud,Pros and Cons so no more explanation for that.

But One might ask how it is possible to share data between applications inside a single device there are some workarounds to achieve that.

iCloud

It's possible because the identifier which is used for iCloud is different from bundle identifier so it's possible to share images,videos and other documents.

To know more see the discussion on this topic


Web Service:

Sync data through third party (e.g. Dropbox) or custom built web service.

web service

Availability: iOS 2.0+

Pros:

  • Useful for sharing and otherwise distributing large amounts of data.

Cons:

  • Requires a network connection.
  • Web service implementation overhead.

Reference


From iOS 8 I've successfully Access Same folder in using "App Group Functionality." I'm extending answer of @siejkowski.

Note: It will work from same developer account only.

For that you have to follow below steps.

  1. first Enable "App Groups" from your developer account.
  2. Generate Provisioning profile. and use it.

Now You have to create Two Apps. Sample Name

  1. Demo_Share_One
  2. Demo_Share_Two

Now We are copying images from Demo_Share_One to Sharing folder which is created by default when you enable App Groups and run app. and will access all those images from Demo_Share_Two.

You have to Take Group Name which was set to your developer account.lets say group.filesharingdemo.

Add Below method in Both apps to get Relative path of sharing folder url.

- (NSString *) getSharedLocationPath:(NSString *)appGroupName {    NSFileManager *fileManager = [NSFileManager defaultManager];    NSURL *groupContainerURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:appGroupName];    return [groupContainerURL relativePath];}

Now we are Copying Images from Bundle from Demo_Share_One

-(IBAction)writeImage:(id)sender{    for (int i = 0; i<15; i++)     {        NSString *strSourcePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hd%d",i+1] ofType:@"jpg"];        NSString *strDestinationPath = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d",i+1]] ;        BOOL filewrite = [[NSFileManager defaultManager]copyItemAtPath:strSourcePath toPath:strDestinationPath error:nil];        if (filewrite)            NSLog(@"File write");        else            NSLog(@"can not write file");    }}

Now in Demo_Share_Two to access those images

NSString *pathShared = [[self getSharedLocationPath:@"group.filesharingdemo"] stringByAppendingPathComponent:[NSString stringWithFormat:@"hd%d.jpg",number]];NSLog(@"%@",pathShared);//BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:pathShared];imgView.image = [UIImage imageWithContentsOfFile:pathShared];

And Now You will get all images which your write from Demo_Share_One.

So From now onwards if you want to share this folder two your third app. just add that app in your group. So it is too easy to access same elements in Your Multiple apps.

if you will not enable App Groups in your AppID then you will get [self getSharedLocationPath:@"group.filesharingdemo"] is null.

Thanks to Apple for Share Elements from your own apps functionality. Happy Coding. :)


Historically, the iPhone has tried to prevent data sharing between apps. The idea was that if you couldn't get at another app's data, you couldn't do anything bad to that app.

In recent releases of IOS, they've loosened that up a bit. For example, the iOS programming guide now has a section on passing data between apps by having one app claim a certain URL prefix, and then having other apps reference that URL. So, perhaps you set your event app to answer "event://" URLs the same way that a webserver answers for "http://" URLs.

Apple's documentation of that approach is here.

Have a peek under "Implementing Custom URL Schemes".