Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the iOS use argc and argv?

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 :)

like image 254
Tim Avatar asked Oct 15 '10 14:10

Tim


People also ask

What is the difference between argc and argv in Linux?

int main (int argc, char *argv []) Here, argc parameter is the count of total command line arguments passed to executable on execution (including name of executable as first argument). argv parameter is the array of character string of each command line argument passed to executable on execution.

What does int argc *argv[] mean in C++?

What does int argc, char *argv [] mean in C/C++? C C++ Server Side Programming Programming argc stands for argument count and argv stands for argument values. These are variables passed to the main function when it starts executing.

How to use command-line arguments (argc and argv) in C?

This article will explain several methods of using command-line arguments, argc and argv, in C. When a program gets executed, the user can specify the space-separated strings called command-line arguments. These arguments are made available in the program’s main function and can be parsed as individual null-terminated strings.


2 Answers

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.

like image 121
Matthias Bauch Avatar answered Oct 12 '22 18:10

Matthias Bauch


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.

like image 27
Nimrod Avatar answered Oct 12 '22 16:10

Nimrod