Share datas between two apps with iOS 8 App Groups (using NSUserDefaults)

后端 未结 2 1224
挽巷
挽巷 2020-12-06 01:55

I wonder if we can share datas between apps with the new iOS 8 feature : App groups (using NSUserDefaults) - Or if App Groups only share datas between the main app and its e

2条回答
  •  天命终不由人
    2020-12-06 02:08

    App Groups no longer work in WatchOS2. You must use the watch connectivity Framework.

    in your iOS App:

    import UIKit
    import WatchConnectivity
    
    class ViewController: UIViewController, WCSessionDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        if (WCSession.isSupported()) {
            let session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()
        }
    
        do {
            let applicationDict = ["key" : "value"]
            try WCSession.defaultSession().updateApplicationContext(applicationDict)
        } catch {
            // Handle errors here
        }
    }
    
    }
    

    In your Watch OS2 App:

    import WatchKit
    import WatchConnectivity
    import Foundation
    
    class InterfaceController: WKInterfaceController, WCSessionDelegate {
    
    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
        print(applicationContext)
    }
    

提交回复
热议问题