views:

109

answers:

5

When you create a default iphone OS project in xCode, you have a main.m in "Other Sources" on the side panel in xCode. How does the int main() in there use argc and argv and why does it need it?

Thanks :)

A: 

I think that they are available just because compilers use them but in iOS case are just useless, since there's not direct way to dinamically add arguments to invoked app on iPhone. Or at least I suppose..

Jack
Hmm, interesting thought. ok thank you for the response. I'm hoping someone has a bit more insight. thanks again!
Tim
+4  A: 

Open the info of your executable. In the arguments tab there is arguments to be passed on launch:. If you add something there, it will be passed to your app.
Apple created some arguments that you put there and they change the behaviour of the app. -com.apple.CoreData.SQLDebug 1 for example will print some sql debug messages if you use coredata. I'm sure there are more debug arguments

int count;
for (count = 0; count < argc; count++)
{
    NSLog(@"argv[%d] = %s\n", count, argv[count]);
}

EDIT: those arguments are only used if you start the app with xcode.

fluchtpunkt
Sorry, I am new to this. Which file is the executable in a default iOS template?
Tim
In your xcode sidebar there are several groups, one is named like your project, and there is Targets, Executables, Find Results and so on. Open the Executables Group, there should be one item, your executable. Double-Click it to get to the info.
fluchtpunkt
Ahh I see. Thank you~
Tim
A: 

This is just a manifestation of Objective-C being a superset of C.

On Mac OS X, you can use this to pass arguments to an app by calling it from Terminal, such as (probably not working):

/Applications/iTunes.app/Contents/MacOS/iTunes my args to application

On iOS, there is no possibility to launch apps from command line.

mouviciel
That's what I was thinking. So they aren't really doing anything in the project, they are just there by default?
Tim
+2  A: 

See: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html

The argc and argv parameters contain any launch-time arguments passed to the application from the system. These arguments are parsed by the UIKit infrastructure and can otherwise be ignored.

Basically UIApplicationMain() creates a singleton UIApplication object which is also handed your Application Delegate object. They don't specify the initialization protocol in the docs, but it sounds like argc/argv get passed to UIApplication during some sort of initialization and then UIApplication parses them and turns them into information (perhaps launch options) that can be accessed through UIApplication.

In any case argc/argv are pretty much reserved for system use in iOS applications. The system seems to use them to pass stuff to UIApplication, essentially.

EDIT

As an experiment, I inserted the following in my main() function:

for (int i; i < argc; i++)
    NSLog(@"%s", argv[i]);

When I launched it in the simulator it simply printed one "argument" (argv[0]) which is the path of the application.

I suspect if you put this in here and launch an application that registers a URL handler or opens because of a local notification or some other system event then you'll see the URL or whatever options relate to how the application was opened. However you are not supposed to parse argc/argv[] yourself! Use the application launch options provided to the UIApplicationDelegate application:didFinishLaunchingWithOptions: method.

Nimrod
I just checked that, and when my app is started via "open in" no arguments are passed to the app.
fluchtpunkt
Oh well, I guess my guess was wrong then. I'm curious though, do you have a complete list of valid debug options like the -com.apple.CoreData.SQLDebug thing? I didn't know that one existed and it might come in handy for me.
Nimrod