Reputation: 8412
I am developing a watchOS extension which uses WCSession
to communicate with the iPhone. However, I do not own an Apple Watch and therefore have to rely on the Watch Simulator to test my code.
Is there a way to test the scenario where the Watch is disconnected from the phone in the Simulator?
If not, is there some documentation or a well-researched blog post that gives some insight into the behavior of WCSession
in this case?
Upvotes: 12
Views: 2219
Reputation: 1
You can simulate the isReachable going to be false by placing the app which is on foregroundState in a backgroundState by pressing one of those two buttons on the watch.
It has the same effect that the iPhone is two far from the watch to be connected ( out of range).
CF: https://developer.apple.com/documentation/watchconnectivity/wcsession/1615683-isreachable
Still today, there isn't a way to simulate the disconnect directly or i haven't find it, the isReachable still be true if your turn off the iPhone simulator in attend to simulate the disconnect.
Just place your function you would test in the sessionReachabilityDidChange(_ session: WCSession) of the delegate and assume it has the same effect.
Upvotes: 0
Reputation: 4666
Quitting the iPhone simulator should let you approximate this scenario.
Upvotes: 3
Reputation: 20187
Quit the iPhone simulator while the Apple Watch simulator is running achieves the specific state that you describe... As with an actual Apple Watch, the Watch Simulator will show a phone disconnected icon at the top of the screen when you quit the iPhone Simulator. See image below.
There is another state to consider, which is that WCSession
only can communicate when the two apps are both in the foreground. Therefore, you should also test the scenario where the Watch and iPhone are connected (both simulators are running) but the iPhone app is not in the foreground. (And likewise, with the iPhone app in the foreground while the Watch is connected but the Watch app is not in the foreground.)
Upvotes: 0
Reputation: 1252
I understand that you actually want a test scenario.
But which direction for communication do you want to test? If you check documentation of WCSession
it always states the behavior for the watch and the iOS device.
Furthermore, what do you mean with 'disconnected'?
You can check for WCSession.defaultSession().reachable
but documentation states
On iOS, the value is YES when the paired Apple Watch is in range and the associated Watch app is running in the foreground.
You can check for paired
, but you also need to check for watchAppInstalled
.
I believe that all communication is asynchronous. Do you want to check your errorHandler:
code as in
- (void)sendMessageData:(NSData *)data
replyHandler:(void (^)(NSData *replyMessageData))replyHandler
errorHandler:(void (^)(NSError *error))errorHandler
I think it is not possible to test it on a simulator. You could only copy your errorHandler code temporarily to replyHandler for testing.
Here is the code I use for testing the availability:
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = _modelController
session.activateSession()
_modelController!.transferArrayToWatchWithSession()
}
and within the _modelController
func transferArrayToWatchWithSession() {
let session = WCSession.defaultSession()
if WCSession.isSupported() && session.watchAppInstalled {
session.transferUserInfo([kWatchControlsDictKey:self.verifiedWatchArray])
} else {
log.info(....")
}
}
Upvotes: 1
Reputation: 8962
You should use an if-query, whether the iPhone is reachable before starting a WC Request:
if (WCSession.defaultSession().reachable) {
//do your request here
} else {
//handle non-reachability
}
If you want to test your app's reaction on a non-reachability just replace WCSession.defaultSession().reachable
with false
.
Upvotes: 1
Reputation: 6394
I dont think so its possible because Apple doc says,
Additionally, WatchKit apps have a reliable connection to the simulated host device because they both are running in the Simulator.
Apple mentioned this in Hardware Difference section of Simulator User Guide
Upvotes: 2