2015-09-18 11 views
11

私は、同じデバイス内のアプリケーション間でデータを共有する作業があります。どちらのアプリでも同じデバイス上の共有データベースを使用できますか? IOSの2つのアプリケーション間でデータを共有する方法誰もが何らかの形でそれをやった。 お知らせください。ありがとうIOSのアプリケーション間でデータを共有

+0

[リンク](http://www.enharmonichq.com/sharing-data-locally-between-ios-apps/)をチェックしてください。これはObjective-Cに基づいていますが、同じ概念がそのまま適用されます。 –

+0

@ Phoen1xUK私は記事が古くなっていると思う。なぜなら、iOS 8、アプリケーショングループはそれを行う公式な方法だからだ。 – neuhaus

+0

私はAppグループについて話しています。あなたはこれを実装しましたか? – Abhishek

答えて

23

同じグループコンテナIDを持つ両方のアプリケーションのApp Project機能タブでAppグループを有効にすることができます。あなたはスイッチの状態を共有したいのであれば

let sharedContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.yourCompanyID.sharedDefaults")! 

:「group.com.yourCompanyID.sharedDefaults」

enter image description here

は、その後、あなたは次のURLを使ってアプリから同じフォルダにアクセスすることができますあなたは次のようにそれを行う必要がある二つの異なるアプリケーションから:

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 ?? false 
    } 
    @IBAction func switched(_ switch: UISwitch) { 
     let success = NSKeyedArchiver.archiveRootObject(switch.isOn, toFile: switchURL.path) 
     print(success) 
    } 
}