How to share data using Watch Connectivity when wo

2020-06-03 00:41发布

问题:

In my iOS application I use Core Data to store data and a fetch request to create an array of NSManagedObjects to display in a UITableView.

On the Watch OS I check if WCSession is supported and active a session, then send the iOS application a message from the watchOS extension.

When the iOS application receives the message from the watchOS it should send the array of Objects to the watchOS extension to display the data in the WKInterfaceTable, but I am unsure how to do this. Ultimately what I am trying to achieve is;

  • How to share the array of Objects with the watchOS extension?

  • If the user adds/edits/deletes objects in the array on the Watch, how can we update the data on the iPhone ?

  • Also, the iOS application is embedded within a UITabBarController so does it matter which view controller I communicate with?

Watch OS FavouritesInterfaceController

var session : WCSession!

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()

    //Check if session is supported and Activate
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    // Interface Objects

    //Send Message
    sendmessagetoiphone()   
}

func sendMessageToIphone() {
    if(WCSession.isSupported()){
        session.sendMessage(["b":"goodBye"], replyHandler: nil, errorHandler: nil)
    }
}

IOS Application : FavouritesViewController

var objects = [Objects]()

func loadData() { 

    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let request = NSFetchRequest(entityName: "Objects")
    request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
    do {
        try
            self.objects = moc.executeFetchRequest(request) as! [Objects]
        // success ...
    } catch {
        // failure
        print("Fetch failed")
    }
 }

   func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    //handle received message   
    let value = message["Value"] as? String
    dispatch_async(dispatch_get_main_queue()) {
        self.messageLabel.text = value
    }
    //send a reply
    replyHandler(["Value":"Hello Watch"])
   }

回答1:

  • How to share the array of Objects with the Watch OS Extension ? Since you are using WatchConnectivity framework, send the array of objects from iPhone using sendMessage method and in your FavoritesInterfaceController implement the func session(session: WCSession, didReceiveMessage method in order to get the response or you can get the array in replyhandler to.

  • If the user adds/edits/deletes objects in the array on the Watch OS how can we update the data on the iPhone ?

    Send the objectId along the with new changes in your sendMessage method from watch to phone, on receiving on phone made the changes in your database save it and send the updated value in your replyHandler so that your watch content will be updated accordingly.

  • Also the iOS application is embedded within a UITabBarController so does it matter which view controller I communicate with ?

    You desired viewController to which you are communicating OR the one that is responsible for doing changes should be alive. If multiple ViewControllers are listening to WCSessionDelegates then when you send any message from watch all of the live controllers will receive that message. You should include some kind of identifier in your sendMessage dictionary so you can know which operation to perform. Like if you want to delete an object then when watch sends a message the identifier will contain delete so that on receiving you can check the identifier value and perform the delete operation.



回答2:

You can use the replyHandler in the sendMessage to do this. Make sure you implement the reply handler on both Watch and iOS App to get this.

Basically, if you get it right, your reply handler can ensure what your iOS app does in response for a watch app's message.

Also, speaking of your response (of sending an array of objects) - you should send it as a dictionary and fetch it on the watch.



回答3:

First of, this is a really good question. For starters I'd recommend that you watch this session from the WWDC 2015: Session 713 - Introducing Watch Connectivity. This can be found here.

Now to your actual question. There is a great tutorial and Github repo that show you how to communicate Core Data between your Apple Watch app and the container app using App Groups, as this enables you to access all shared content, such as Core Data and even NSUSerdefaults.

You can then find the complete tutorial on how to do this under the following link.

Hope that helps, Julian.