diff --git a/Mini vMac WatchKit Extension/InterfaceController.m b/Mini vMac WatchKit Extension/InterfaceController.m index bde7fb8..2f96e5b 100644 --- a/Mini vMac WatchKit Extension/InterfaceController.m +++ b/Mini vMac WatchKit Extension/InterfaceController.m @@ -49,6 +49,7 @@ static NSObject *sharedEmulator = nil; @implementation InterfaceController { WKExtendedRuntimeSession *runtimeSession; + BOOL hasStartedEmulator; } + (void)load { @@ -121,13 +122,14 @@ static NSObject *sharedEmulator = nil; - (void)willActivate { if ([WKExtension sharedExtension].applicationState == WKApplicationStateActive) { - sharedEmulator.running = YES; + [self startRuntimeSession]; } } - (void)didDeactivate { // This method is called when watch view controller is no longer visible [super didDeactivate]; + [runtimeSession invalidate]; sharedEmulator.running = NO; } @@ -135,6 +137,21 @@ static NSObject *sharedEmulator = nil; //uint32_t connected = session.activationState == WCSessionActivationStateActivated && session.reachable; } +- (void)startRuntimeSession { + runtimeSession = [WKExtendedRuntimeSession new]; + runtimeSession.delegate = self; + [runtimeSession start]; +} + +- (void)startOrResumeEmulator { + if (!hasStartedEmulator) { + hasStartedEmulator = YES; + [sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO]; + } else { + sharedEmulator.running = YES; + } +} + - (void)loadAndStartEmulator { #ifdef LTOVRTCP_SERVER setenv("LTOVRTCP_SERVER", LTOVRTCP_SERVER, 1); @@ -165,16 +182,11 @@ static NSObject *sharedEmulator = nil; CGFloat scale = [self bestScaleForScreen]; [sharedEmulator.screenLayer setAffineTransform:CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI_2), scale, scale)]; [sharedEmulator.screenLayer setMinificationFilter:@"CAFilterTrilinear"]; -#if TARGET_OS_SIMULATOR - [sharedEmulator performSelector:@selector(run) withObject:nil afterDelay:0.1]; -#endif TrackPad *trackpad = [[TrackPad alloc] initWithFrame:fullScreenView.bounds]; [fullScreenView addSubview:trackpad]; - runtimeSession = [WKExtendedRuntimeSession new]; - runtimeSession.delegate = self; - [runtimeSession start]; + [self startRuntimeSession]; } - (CGFloat)bestScaleForScreen { @@ -208,8 +220,10 @@ static NSObject *sharedEmulator = nil; error:NULL]; [[AVAudioSession sharedInstance] activateWithOptions:0 completionHandler:^(BOOL activated, NSError * _Nullable error) { // network only works on watchOS when there's an active audio session - [sharedEmulator performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:NO]; + [self startOrResumeEmulator]; }]; +#else + [self startOrResumeEmulator]; #endif }