1

How can i run a shell command in objective c with a variable E.G system("thisCMD thisParam %@", username);

1 Answer 1

1
[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:username]];

Or if username isn't already a string:

[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:[username description]]];

Or if you want to be able to change the format specifier for each argument to something more complicated later:

[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:[NSString stringWithFormat:@"%@", username]]];
Sign up to request clarification or add additional context in comments.

2 Comments

lets say the command someCommand usernameShouldBeHere anotherparam how do i do it then?
Change thisCMD to someCommand and construct the NSArray using the arrayWithObjects constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.