Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在IOS中的应用程序之间共享数据_Ios_Swift_Data Sharing - Fatal编程技术网

在IOS中的应用程序之间共享数据

在IOS中的应用程序之间共享数据,ios,swift,data-sharing,Ios,Swift,Data Sharing,我的任务是在同一设备中的应用程序之间共享数据。可能两个应用程序都可以在同一设备上使用共享数据库。如何在IOS中的两个应用程序之间共享数据。任何人都以任何方式做过这件事。 请让我知道。谢谢您可以使用相同的组容器ID“group.com.yourCompanyID.sharedDefaults”在两个应用程序的“应用程序项目功能”选项卡上打开“应用程序组” 然后,您可以使用以下url从应用程序访问同一文件夹: let sharedContainerURL = FileManager.default

我的任务是在同一设备中的应用程序之间共享数据。可能两个应用程序都可以在同一设备上使用共享数据库。如何在IOS中的两个应用程序之间共享数据。任何人都以任何方式做过这件事。
请让我知道。谢谢

您可以使用相同的组容器ID“group.com.yourCompanyID.sharedDefaults”在两个应用程序的“应用程序项目功能”选项卡上打开“应用程序组”

然后,您可以使用以下url从应用程序访问同一文件夹:

let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")!
因此,如果您想从两个不同的应用程序共享切换状态,您应该按照以下操作:

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)
    }
}

退房它基于Objective-C,但同样的概念仍然适用。@Phoen1xUK我认为这篇文章已经过时了,因为iOS 8,应用程序组是实现这一目标的官方方式。我指的是应用程序组。你实施了这个吗?