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.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments