Start-up function of a program
I want to execute a code snippet when program start-up, so what is the start-up function of iOS program?
For Android program, the start-up function is onCreate as below
public class HelloWorld extends Activity {
public void onCreate(Bundle 开发者_StackOverflowsavedInstanceState) {
...
How about iOS program?
Thanks
In iOS you use the UIApplicationDelegate protocol in order to be informed of application lifecycle events (startup, suspend, shutdown, etc.):
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
}
@end
@implementation MyAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//start-up code here
return YES;
}
@end
Assuming that you are using XCode for your iOS project, it will have created a default UIApplicationDelegate
class for you. All you need to do is find it and edit the application:didFinishLaunchingWithOptions:
method to your liking.
The main
function is called to begin the program. It is found in main.m
in the templates in Xcode. You can also use __attribute__((constructor))
to mark a function for execution before the program begins, or create a +load
or +initialize
method on a class. +load
methods are called when a class is loaded into memory, before the program begins, but not everything will be loaded at that point. +initialize
is called automatically before any other method in the class it is defined in, after the program starts.
精彩评论