简体   繁体   中英

Is there any delegate that fires when I run the iphone application for the first time?

I need to track the download of a certain iphone application. I tried a lot and found out that we could track it from the AppStore. But i need to track that from my application itself. So please help me to identify the method that fires when the application starts for the first time. Thanks.

There's no specific method that fires only on the 1st application launch. You can set a flag in user defaults on application start - so if the flag is not present then that will mean that application launched for the 1st time:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    if (![[NSUserDefaults standardDefaults] boolForKey:@"AlreadyLaunched"]){
        // First launch logic

        [[NSUserDefaults standardDefaults] setBool:YES forKey:@"AlreadyLaunched"];
        [[NSUserDefaults standardDefaults] synchronize];
    }
    ...
}

But i need to track that from my application itself.

No.

But if you really want to do this you could use something like this:

BOOL hasUsedSpyWareFunctions = [[NSUserDefaults standardUserDefaults] boolForKey:@"SpyWareKey"];
if (!hasUsedSpyWareFunctions) {
    [self spyOnUser];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SpyWareKey"];
}

if you are a Pro in spying you only set the key to YES if the method returned successfully (ie a network connection could be established)

There's no such an event, at least not one that I know of. But what you want can be trivially done using NSUserDefaults . Simply check for some boolean flag and if it's not there, it's a first run and you can set the flag:

NSString *const AlreadyRunKey = @"already-run";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs boolForKey:AlreadyRunKey]) {
    [prefs setBool:YES forKey:AlreadyRunKey];
    [prefs synchronize];
    // do whatever else you want
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM