Abh*_*hek 18 data-sharing ios swift
我有一项任务是在同一设备中的应用程序之间共享数据.可能两个应用程序都可以在同一设备上使用共享数据库.如何在IOS中的两个应用程序之间共享数据.任何人都以任何方式做到了.请告诉我.谢谢
Leo*_*bus 27
您可以在具有相同组容器ID的两个应用程序的应用程序项目功能选项卡上打开应用程序组."group.com.yourCompanyID.sharedDefaults"
然后,您可以使用以下网址从您的应用访问同一文件夹:
let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")!
Run Code Online (Sandbox Code Playgroud)
因此,如果您想要从两个不同的应用程序共享切换状态,您应该按以下方式执行:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4727 次 |
| 最近记录: |