PDA

View Full Version : system(someString) causing problems




lynkynpark86
Jan 29, 2011, 06:55 AM
I'm trying to get better at Obj-C, and I'm making a series of simple apps. For developers of cydia apps, I'm sure you've heard of the Erica Utilities (http://ericasadun.com/ftp/EricaUtilities/). I'm using the urlclip utility to make an app that makes a webclip. Here's my code:

-(IBAction)makeClip {

urlString = [[NSString alloc] initWithFormat:urlField.text];
nameString = [[NSString alloc] initWithFormat:nameField.text];
cmdString = [[NSString alloc] initWithFormat:@"urlclip %@ %@", nameString, urlString];
system(cmdString);

}

But my system(cmdString) line is getting a warning: "Passing argument 1 of 'system' from incompatible pointer type". Is it because I'm using a string within system()? I tested it on my device, and it isn't working. The cmdString string is working right, as I tested it with a UIAlertView. The problem IS in the system(). Any ideas?



Guiyon
Jan 29, 2011, 08:31 AM
The warning is GCC telling you are trying to call the system function in a way that does not match it's definition (and it is completely correct). system(3) is a function from the Standard C library and expects C (not Objective-C) datatypes. I would suggest opening up a Terminal window and typing:
man system

and read the function's man page to get some idea about how it's declared, the number of type of it's arguments, etc.

lynkynpark86
Jan 29, 2011, 09:22 AM
I read the man page, and what I gathered is that I should be using sh(), not system(). I tried sh(), and I am now getting an actual error, not a warning. I guess what I'm saying is, how do I run a string as a command on the iPhone?

Guiyon
Jan 29, 2011, 09:29 AM
Re-read the man page (specifically the 'Synopsis' section), the function you are using is correct but you are not calling it with the correct arguments. If you are still stuck after that, here's another hint: Take a look at the NSString class reference, one of it's methods will convert the string into the form you need.

lynkynpark86
Jan 29, 2011, 06:39 PM
Re-read the man page (specifically the 'Synopsis' section), the function you are using is correct but you are not calling it with the correct arguments. If you are still stuck after that, here's another hint: Take a look at the NSString class reference, one of it's methods will convert the string into the form you need.

Are you talking about const char? I'm assuming it's not int you're talking about. So I need NSStringEncoding? How would I type that? I'm sorry I'm asking you so spell it out for me, but I am very new to programing.

bredell
Jan 30, 2011, 01:19 PM
I'm pretty sure you're not allowed to use the system(3) call on an iOS device. system(3) uses the fork(2) call, and that call is blocked by Apple.

But don't take my word for it, last time I tried using it was almost a year ago and things might have changed since then.