简体   繁体   English

首次运行iphone应用程序时会激发任何代表吗?

[英]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. 我需要跟踪某个iPhone应用程序的下载。 I tried a lot and found out that we could track it from the AppStore. 我尝试了很多,发现可以从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) 如果您是间谍专家,则只有在方法成功返回(即可以建立网络连接)的情况下,才将密钥设置为YES

There's no such an event, at least not one that I know of. 没有这样的事件,至少我不知道。 But what you want can be trivially done using NSUserDefaults . 但是,您可以使用NSUserDefaults完成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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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