xcode 仅在第一次运行 ios 时执行函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12853283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:47:46  来源:igfitidea点击:

Execute function only at first run ios

objective-ciosxcode

提问by Eldhose

Is there any way by which i can run a function only once (when the app is updated or installed)? I can't use run script as i should use objective c function.

有什么方法可以让我只运行一次函数(当应用程序更新或安装时)?我不能使用运行脚本,因为我应该使用目标 c 函数。

回答by Bhupendra

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    if ( ![userDefaults valueForKey:@"version"] )
    {
            // CALL your Function;

            // Adding version number to NSUserDefaults for first version:
          [userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];    
    }


    if ([[NSUserDefaults standardUserDefaults] floatForKey:@"version"] == [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] )
    {
    /// Same Version so dont run the function
    }
    else
    {
       // Call Your Function;

       // Update version number to NSUserDefaults for other versions:
         [userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
    }

回答by Lester

This is the Swift conversion of Bhupendra's answer:

这是Bhupendra 答案的 Swift 转换:

let infoDictionary: NSDictionary? = NSBundle.mainBundle().infoDictionary // Fetch info.plist as a Dictionary
let temp: NSString = infoDictionary?.objectForKey("CFBundleVersion") as NSString
let val: Float = temp.floatValue

var userDefaults = NSUserDefaults()
if userDefaults.valueForKey("version") == nil {            
    // Call function
    // Adding version number to NSUserDefaults for first version
    userDefaults.setFloat(val, forKey: "version")
}

if NSUserDefaults().floatForKey("version") == val {
    // Same Version so dont run the function
} else {    
    // Call function
    // Update version number to NSUserDefaults for other versions
    userDefaults.setFloat(val, forKey: "version")
}

回答by David R?nnqvist

If you want to run some code when the app is either first installed or after every update then you could read the current version of your application from your bundle

如果您想在首次安装应用程序或每次更新后运行一些代码,那么您可以从您的包中读取应用程序的当前版本

CGFloat currentVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue];

and write that to user defaults after you have run the code.

并在运行代码后将其写入用户默认值。

[[NSUserDefaults standardUserDefaults] setFloat:currentVersion 
                                         forKey:kLastVersionThatExecutedMyCodeKey];

The next time you start your app, you compare the value from the user defaults against the bundle version. If the bundle version has changed the app was updated which means that you should run the code again and write the new version to user defaults.

下次启动应用程序时,将用户默认值与捆绑版本的值进行比较。如果捆绑版本已更改,则应用程序已更新,这意味着您应该再次运行代码并将新版本写入用户默认值。

回答by Kjuly

You can create a BOOL value key (e.g. isFirstLaunch = NO) in your NSUserDefaults, and set it to YES after you've executed the function.

您可以在您的 中创建一个 BOOL 值键(例如isFirstLaunch = NONSUserDefaults,并在执行该函数后将其设置为 YES。

If you want to execute it every time the user launch the App, you'll need to set the key to default value before the App exists (i.e. reset it in -applicationWillTerminate:method in AppDelegate).

如果您想在每次用户启动应用程序时执行它,您需要在应用程序存在之前将密钥设置为默认值(即-applicationWillTerminate:在 AppDelegate中的方法中重置它)。

回答by amagain

This is the Swift2.0 version of Bhupendra's Answer, if anyone needs it!

这是 Bhupendra's Answer 的 Swift2.0 版本,如果有人需要的话!

    let infoDictionary: NSDictionary? = NSBundle.mainBundle().infoDictionary
    //Fetch info.plist as a Dictionary
    let temp: NSString = infoDictionary?.objectForKey("CFBundleVersion") as! NSString
    let val: Float = temp.floatValue

    let userDefaults = NSUserDefaults()
    if userDefaults.valueForKey("version") == nil {
        userDefaults.setFloat(val, forKey: "version")
    }
    if NSUserDefaults().floatForKey("version") == val {
        //if it is the same version, don't run the code
    }
    else {
        userDefaults.setFloat(val, forKey: "version")
    }
}