Abhishek Abhishek - 2 years ago 140
iOS Question

Sharing data in between apps in IOS

I have a task to share data between apps in the same device. May be both apps can use a shared database on same device. How to share Data between two apps in IOS. Anybody have done it in any way.
Please let me know. Thanks

Answer Source

You can turn on App group on your App Project capabilities tab on both of your apps with the same group cointainer ID. "group.com.yourCompanyID.sharedDefaults"

enter image description here

Then you can access the same folder from your apps using the following url:

let sharedContainerURL = NSFileManager().containerURLForSecurityApplicationGroupIdentifier("group.com.yourCompanyID.sharedDefaults")!

So if you would like to share a switch state from two different apps you should do it as follow:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var sharedSwitch: UISwitch!

    let sharedContainerURL = NSFileManager().containerURLForSecurityApplicationGroupIdentifier("group.com.yourCompanyID.sharedDefaults")!
    let filePath = NSFileManager().containerURLForSecurityApplicationGroupIdentifier("group.com.yourCompanyID.sharedDefaults")!.URLByAppendingPathComponent("switchState.plist").path!

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateSwitch", name: UIApplicationDidBecomeActiveNotification, object: nil)

    func updateSwitch() {
        sharedSwitch.on = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? Bool ?? false

    override func didReceiveMemoryWarning() {

    @IBAction func switched(sender: UISwitch) {
        let success = NSKeyedArchiver.archiveRootObject(sender.on, toFile: filePath)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download