Search code examples
iosiphoneswiftwatchkitwatchconnectivity

Adding Watchkit Extension and letting the iPhone do the work


I need to take on an iPhone App and implement a Watchkit Extension to it. Now here's the thing: There are really a lot of classes, especially those which are responsible for network communication (authentication tokens, etc.). Now the guys who built the app were doing things like showing UIAlerts from the Network communication like this:

class func showReloadDataDialog(_ action: @escaping (_ action: UIAlertAction) -> Void) {
    let alert = UIAlertController.init(title: String.localizedString("AlertTitleError", ""),
                                       message: String.localizedString("ActivationRetrySupportedNetworksLoadMessage", ""),
                                       preferredStyle: .alert)
    let defaultAction = UIAlertAction.init(title: String.localizedString("AlertNoButton", ""), style: .default, handler: nil)
    alert.addAction(defaultAction)
    let reloadAction = UIAlertAction.init(title: String.localizedString("AlertYesButton", ""), style: .default, handler: action)
    alert.addAction(reloadAction)
    self.present(alert)
}

And here's the present() function:

// MARK: Present
class func present(_ alert: UIAlertController) {
    if let window = UIApplication.shared.delegate?.window, var topController = window!.rootViewController {
        while topController.presentedViewController != nil {
            topController = topController.presentedViewController!
        }
        if let navigationController = topController as? UINavigationController {
            topController = navigationController.visibleViewController!
        }
        if topController is UIAlertController {
            return
        }
        topController.present(alert, animated: true, completion: nil)
    }
}

As you can see, they were doing things a little bit different than they should have been done. Now this is really a problem, because I cannot use this Network communication class in my Watchkit extension, because there is no UIViewController in Apple's Watchkit - which means it won't compile when I add this class to my watch target.

This is just an example, there are many other things like that and I really don't want to refactor the whole source.

Now my question is: Can I somehow let the iPhone do all the work (e.g. fetching some data from the server while taking care of all the authentication token stuff) and then just push the result to the Watchkit Extension? I've read some things about WCSession - can I use this for my case? I'm basically trying to display some POIs on a WKInterfaceMap.


Solution

  • As for your first question, You'll find your solution here regarding web service in watchkit. In the link it's recommended by the community to let the iPhone app do the hard work (fetching data etc) and just return the information to watchApp.

    Second, you can use watchconnectivity framework or you can use MMWormhole for communication with your iPhone App. All you need to do is fetch the Place of Interests data and send it to your watchApp. From there you can populate your map.