ioswatchkitapple-watchwatchos-3wcsession

watchOS 3 Using WCSession in class shared between device and watch


My app depends on constant communication between the watch and the device. I want to use a singleton to manage all messaging and session states. I want to use inheritance to further customize this behavior separately on the watch and on the device. I want the parent class to be a WCSessionDelegate, but the WCSession library exists for iOS and watchOS and each has separate delegate functions. Is there a way to include both sets of functions in the super class? Right now the compiler treats the file as one or the other even though it is a part of both the app and watch targets.

Functions are marked as unavailable


Solution

  • Theoretically, you can use compiler directives like this (Swift3.1):

    #if os(iOS)
        <methods avaliable on iOS's Watch Connectivity framework>
    #elseif os(watchOS)
        <methods avaliable on watchOS's Watch Connectivity framework>
    #endif