NSUserDefaults not working on Xcode beta with Watch OS2

前端 未结 5 1731
一个人的身影
一个人的身影 2020-11-27 07:02

I just installed the latest beta of Xcode to try Swift 2 and the improvements made to the Apple Watch development section.

I\'m actually having an h

5条回答
  •  难免孤独
    2020-11-27 07:36

    As mentioned already, shared NSUserDefaults no longer work on WatchOS2.

    Here's the swift version of @RichAble's answer with a few more notes.

    In your iPhone App, follow these steps:

    Pick the view controller that you want to push data to the Apple Watch from and add the framework at the top.

    import WatchConnectivity
    

    Now, establish a WatchConnectivity session with the watch and send some data.

    if WCSession.isSupported() { //makes sure it's not an iPad or iPod
        let watchSession = WCSession.defaultSession()
        watchSession.delegate = self
        watchSession.activateSession()
        if watchSession.paired && watchSession.watchAppInstalled {
            do {
                try watchSession.updateApplicationContext(["foo": "bar"])
            } catch let error as NSError {
                print(error.description)
            }
        }
    }
    

    Please note, this will NOT work if you skip setting the delegate, so even if you never use it you must set it and add this extension:

    extension MyViewController: WCSessionDelegate {
    
    }
    

    Now, in your watch app (this exact code works for Glances and other watch kit app types as well) you add the framework:

    import WatchConnectivity
    

    Then you set up the connectivity session:

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
        let watchSession = WCSession.defaultSession()
        watchSession.delegate = self
        watchSession.activateSession()
    }
    

    and you simply listen and handle the messages from the iOS app:

    extension InterfaceController: WCSessionDelegate {
    
        func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
            print("\(applicationContext)")
            dispatch_async(dispatch_get_main_queue(), {
                //update UI here
            })
        }
    
    }
    

    That's all there is to it.

    Items of note:

    1. You can send a new applicationContext as often as you like and it doesn't matter if the watch is nearby and connected or if the watch app is running. This delivers the data in the background in an intelligent way and that data is sitting there waiting when the watch app is launched.
    2. If your watch app is actually active and running, it should receive the message immediately in most cases.
    3. You can reverse this code to have the watch send messages to the iPhone app the same way.
    4. applicationContext that your watch app receives when it is viewed will ONLY be the last message you sent. If you sent 20 messages before the watch app is viewed, it will ignore the first 19 and handle the 20th one.
    5. For doing a direct/hard connection between the 2 apps or for background file transfers or queued messaging, check out the WWDC video.

提交回复
热议问题