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
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.
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.
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.
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.
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"
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) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With