Share data between two or more iPhone applications

Kamleshwar picture Kamleshwar · Feb 24, 2012 · Viewed 74.9k times · Source

Is this possible to share data between two applications on the same device?

Or can I allow some other application to use my application's information / data or in any other way?

For example, the first application is for event management, and I use it to save some event. The second application is for reminders, which will get data from the other application in order to remind me about the event.

This is just a simple example, not a real scenario.

Answer

Durai Amuthan.H picture Durai Amuthan.H · Nov 18, 2013

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 of applications.

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