Flow of sending data from iOS to watchOS

Discussion in 'iOS Programming' started by patent10021, Jul 12, 2018.

  1. patent10021 macrumors 68030


    Apr 23, 2004
    I'm not talking about WatchConnectivity demo apps where someone clicks a button in iOS and the message gets sent to watchOS. I'm talking about actual chat messages.

    In a major app like FB Messenger, iMessage, WhatsApp etc how are both chat messages and push notifications sent from iOS to watchOS? What is going on in code?

    A messaging app usually has a MessageViewVC, MessagesListVC and the push notifications come from either the native server or a third party service like OneSignal. Are push notifications and actual chat messages sent the same way? Of course we need to implement WatchConnectivity but I'd like to know where exactly in iOS and watchOS would this be taking place? Is it just using WatchConnectivity from iOS tableView to watchOS tableView? Or is this done at the point where iOS is receiving server data?

  2. Mascots macrumors 68000


    Sep 5, 2009
    At one point it made sense to use WC to pipe messages from iOS to watchOS however that is not the case as watch apps must start to be independent.

    Your watch extension should be capable of maintaining its own persistent connect to the server and handle the incoming and outgoing messages through a (for example) a shared framework.
  3. patent10021 thread starter macrumors 68030


    Apr 23, 2004
    But everywhere you look everyone is using WC. What shared framework are you talking about? You mean AlamoFire or something? You're saying all data for example, chat user info, chat messages and chat notifications should be sent from server to watchOS directly?

    I know you can make network calls directly from the watch using NSURLSession but that is assuming there is cellular/Wifi. Most watches in the wild are still series 2,3 without cellular/WiFi.
  4. Mascots macrumors 68000


    Sep 5, 2009
    No - what I meant as a side note moreorless became the topic of my last post: I am saying that if you are writing from scratch, I highly suggest you plan forward or else maintenance in watchOS will become a disaster. I had this happen once before and it's never fun.

    To answer your question of where these functions are occurring: they are being isolated into a shared framework which can be used in the various forms of your app. While you should be taking advantage of WC for communications in the extension when possible, standardizing central components and their connections to remotes will make things much better in the long run and lead to consistency.

    You'll need to compartmentalize your interactions and find which is efficient - I can't really build your architecture for you since it isn't an all or nothing equation but it does sound like you are not separating your model layer cleanly from your controllers.
  5. grandM, Jul 27, 2018
    Last edited: Jul 27, 2018

    grandM macrumors 65816


    Oct 14, 2013
    mascots is saying you should create a separate class/structure
    personally I am tempted to create a model A getting/setting data in the cloud fi through alamofire
    you would also have a network controller B which checks which possibilities the watch got and acts accordingly
    if the watch has cellular/wifi call for A else WC
    in your tableview controller call for B
    in the long run the watch will be responsible for cloud communication
    if your design pattern mvc/mvvm is done right this will have few repercussions
    do you agree, mascots?
  6. patent10021 thread starter macrumors 68030


    Apr 23, 2004
    I am relatively new to developing apps with networking so please help me understand this at a high level. Stackoverflow gives code answers of course but it's difficult to post generic questions about logic and flow.

    Let's say my company is Facebook as an example using WatchOS SDK.

    [A] Handling networking
    To paraphrase:
    1. With the new cellular capable watch first you should check for cellular/WiFi/Bluetooth.
    Question - I would create a separate CheckNetworkController class that checks for the connection type? Or is it normally done in ExtensionDelegate?
    2a. If AppDelegate detects cellular, use the CellularController.
    2b. If AppDelegate detects no cellular, use the NormalController; the watch then uses WC to send the message context to iOS then iOS sends the message to the server?

    Chat Messages
    Bob sends Alice a message and it is sent to Facebook servers just like in Parse or Firebase.
    2. That message is stored in Facebook's database like Parse's mLAb database or Firebase's own Firestore.
    3. The server then relays that message to Alice.
    Question - What method in watchOS/iOS is used for sending the chat message to the server?
    Question - How is that chat message relayed to Alice from the Facebook server? For example what code on the server would be responsible for sending that message to Alice? It would depend on the server's language I guess eg C++, Node.js etc?

    [C] Remote push notifications
    Facebook sends a remote push notification to Alice letting her know Bob sent a message (UNNotificationCategory "messageCat"). First it gets relayed from Facebook's server to Apple's apns servers. If the certificates and tokens match it gets sent to Alice's device.
    Question - How does the Facebook server know it needs to send that push notification to Alice?
    Question - How is that push notification sent to Apple apns server?

    Facebook server sends a notification to Alice that Bob is in her vicinity (UNNotificationCategory "vicinityCat").
    Question - How does the Facebook server know that Bob is in Alice's area?
  7. Mascots macrumors 68000


    Sep 5, 2009
    1. Yep, imparitive to get ahead of that curve. There are also instances in which the watch will become disconnected from a phone but maintain connection to the internet through local WiFi - that’s a similar scenario to plan for.
    2. You should be wrapping all of this into separate objects that are small, have limited functionality, and can be tested. If using Objective-C, inheriting from NSObject. Then this the work can be handled by these objects outside of your extension delegate (which is only responsible for directing or delegating the work). I typically suffix objects like this with “coordinator”. But yes, you are on a more modular path.
    I can’t help too much of chat relay systems as it is something outside of my experiences. All of the settings and configuration is handled by facebooks servers and then when a message is deemed ready to be sent, they most likely Curl it to APNS which then pushes it to iPhone and Apple Watch as fit. From there the device can intercept and pass the message to the app which can respond.
  8. jgaz macrumors member


    Dec 30, 2007
    You shouldn't need to worry about which connection your network requests are going through. If you use URLSession the system will handle automatically for you. See : https://developer.apple.com/documentation/watchkit/keeping_your_watchos_app_s_content_up_to_date

    "Your watchOS app can connect directly to web services and other online sources. When making these requests, the system can send it through a paired iPhone as a proxy, over a known WiFi network, or over the watch’s own cellular connection (see Testing watchOS Networking)."

Share This Page