Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data in between apps in IOS

Tags:

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

like image 251
Abhishek Avatar asked Sep 18 '15 13:09

Abhishek


People also ask

Can iOS apps share data?

App Groups are the scheme iOS uses to allow different apps to share data. If the apps have the right entitlements and proper provisioning, they can access a shared directory outside of their normal iOS sandbox.

Can iOS app access other apps?

No. All third-party iOS applications must have a sandbox, and the sandbox prevents direct access to any other app's data. Two apps from the same publisher can create a shared location that both can access, but even then, any data stored in an app's own directories is never exposed to other apps.

Do apps share data with other apps?

It might surprise you to know that 52% of apps share your data with third parties. This data can include things like your location, browsing history, contact details, fitness levels, banking details, and so on.

Can we share apps from iOS to iOS?

You can transfer all your apps to a new iPhone from an iCloud backup during initial setup. Before transferring the apps using iCloud, make sure you've made an iCloud backup of your old phone. You can also use the App Store to choose which apps you'd like to download on your new iPhone.


1 Answers

You can turn on App group on your App Project capabilities tab on both of your apps with the same group container 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 = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "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 switchURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")!         .appendingPathComponent("switchState.plist")     override func viewDidLoad() {         super.viewDidLoad()         print(switchURL.path)         NotificationCenter.default.addObserver(self, selector: #selector(updateSwitch), name: .UIApplicationDidBecomeActive, object: nil)     }     func updateSwitch(_ notofication: Notification) {         sharedSwitch.isOn = NSKeyedUnarchiver.unarchiveObject(withFile: switchURL.path) as? Bool == true     }     @IBAction func switched(_ sender: UISwitch) {         let success = NSKeyedArchiver.archiveRootObject(sender.isOn, toFile: switchURL.path)         print(success)     } } 
like image 197
Leo Dabus Avatar answered Sep 19 '22 04:09

Leo Dabus