0

In my little experience I noticed that creating two or more Interface Controller (IC) for a WatchKit app, the AppleWatch start to load the next interface before the user swipe to it. This could be useful for system performances, but then there is something confusing me. For what I have understood each IC should have its own WCSession to communicate with the paired iPhone, but I'm starting thinking this is not true because debugging I saw that if for example the first IC use sendMessage to send a request, the answer is received by the didReceiveMessage of the second IC not the first IC. I didn't tried it yet, but maybe I should create the WCSession only in the first IC, and also if I am on another IC the app fire it in the didReceiveMessage of the first. If this is right, how can I do something in the second IC when a message arrives? In WatchKit I don't think I can use Observers. Can someone clarify me this, please?

Doing some more search over internet I've found this: http://www.sneakycrab.com/blog/2015/5/26/wkinterfacecontroller-lifecycle-in-watchos-101 This is the confirmation about the preload of IC, that still the same also in WatchOS 2.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Nicola
  • 61
  • 1
  • 1
  • 4

3 Answers3

1

You are using WCSession.defaultSession, which is fine, and as Apple recommends, but it means that session points to the same object in both WKInterfaceControllers, so session.delegate = self by design sets the session's delegate to be whichever WKInterfaceController has been loaded (not displayed) most recently. As you discovered, WKInterfaceControllers often load before they are specifically required—something Apple explicitly documents.

The correct approach to address this is to set the session delegate in the didAppear method of each WKInterfaceController, rather than in willActivate. This will ensure that the visible interface controller is always the current delegate.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
0

Using global public variables now I'm able to have the correct session fired.

----- myVariables.swift -----
public var showPage1: Bool = false

----- myMainIC.swift -----
override func willActivate() {
    super.willActivate()
    showPage1 = false
}

----- mySecondIC.swift -----
var session: WCSession?
override func WillActivate() {
    super.willActivate()
    // this way I can manage the pre-load
    if (showPage1 == false) {
        showPage1 = true
        return
    }
    // now I can create the session
    if WCSession.isSupported() {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

Because of system pre-load explained in the above article, I should "teach" the mySecondIC to not create the session the first time the page is activate cause is invisible.

Nicola
  • 61
  • 1
  • 1
  • 4
0

Send Data using transferCurrentComplicationUserInfo and receive data in watchkit using - (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary *)userInfo

method.