diff --git a/FBDeviceControl/Commands/FBDeviceApplicationCommands.m b/FBDeviceControl/Commands/FBDeviceApplicationCommands.m index 727bbd65f..5606df3df 100644 --- a/FBDeviceControl/Commands/FBDeviceApplicationCommands.m +++ b/FBDeviceControl/Commands/FBDeviceApplicationCommands.m @@ -237,6 +237,26 @@ - (instancetype)initWithDevice:(FBDevice *)device } - (FBFuture> *)launchApplication:(FBApplicationLaunchConfiguration *)configuration +{ + if (configuration.launchMode == FBApplicationLaunchModeFailIfRunning) { + return [[self processIDWithBundleID:configuration.bundleID] onQueue:self.device.asyncQueue chain:^ (FBFuture* processIdQueryResult) { + if (processIdQueryResult.state == FBFutureStateDone) { + return [[FBDeviceControlError + describeFormat:@"Application %@ already running with pid %@", configuration.bundleID, processIdQueryResult.result] + failFuture]; + } else if (processIdQueryResult.state == FBFutureStateFailed) { + return (FBFuture*)[self launchApplicationIgnoreCurrentState:configuration]; + } else { + return (FBFuture*)processIdQueryResult; + } + }]; + } + return [self launchApplicationIgnoreCurrentState:configuration]; +} + +#pragma mark Private + +- (FBFuture> *)launchApplicationIgnoreCurrentState:(FBApplicationLaunchConfiguration *)configuration { return [[[self remoteInstrumentsClient] @@ -248,8 +268,6 @@ - (instancetype)initWithDevice:(FBDevice *)device }]; } -#pragma mark Private - - (FBFuture *)killApplicationWithProcessIdentifier:(pid_t)processIdentifier { return [[self diff --git a/FBDeviceControl/Management/FBInstrumentsClient.m b/FBDeviceControl/Management/FBInstrumentsClient.m index 312efcccf..c2830dabf 100644 --- a/FBDeviceControl/Management/FBInstrumentsClient.m +++ b/FBDeviceControl/Management/FBInstrumentsClient.m @@ -140,7 +140,8 @@ - (instancetype)initWithConnection:(FBAMDServiceConnection *)connection channels onQueue:self.queue resolveValue:^ NSNumber * (NSError **error) { NSDictionary *options = @{ @"StartSuspendedKey": @(configuration.waitForDebugger), - @"KillExisting": @(configuration.launchMode != FBApplicationLaunchModeFailIfRunning), + // FBApplicationLaunchModeFailIfRunning needs to be taken care of prior to this call. + @"KillExisting": @(configuration.launchMode == FBApplicationLaunchModeRelaunchIfRunning), }; ResponsePayload response = [self onChannelIdentifier:ProcessControlChannel