2012-10-12 61 views

回答

43
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"]; 
    } 
+0

謝謝Bhupendra – Eldhose

+0

歡迎:) – Bhupendra

+0

破解的東西@Bhupendra,我試圖寫一個plist設置文件到用戶文檔商店,你的方式好多了 – MagicalArmchair

2

您可以在NSUserDefaults中創建BOOL值鍵(例如isFirstLaunch = NO),並在執行該功能後將其設置爲YES。

如果您想在用戶每次啓動應用程序時執行此操作,則需要在應用程序存在之前將其設置爲默認值(即將其重置爲AppDelegate中的-applicationWillTerminate:方法)。

3

如果你想運行一些代碼時,應用程序或者是第一次安裝或每次更新後,那麼你可以從你的包

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

閱讀應用程序的當前版本,之後你寫的用戶默認已經運行了代碼。

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

下一次啓動應用程序時,將比較用戶默認值與捆綁版本的值。如果軟件包版本已更改,則應用程序已更新,這意味着您應該再次運行代碼並將新版本寫入用戶默認值。

+0

應該是(缺少引號) - > [[NSUserDefaults的standardUserDefaults]的setFloat:CURRENTVERSION forKey:@ 「kLastVersionThatExecutedMyCodeKey」]; – mikemike396

+0

@ Pyraego.com用一個小的'k'前綴一個變量名是一個常用的約定來表明它是一個常量。 Apple的框架在很多地方使用它。 'kLastVersionThatExecutedMyCodeKey'應該是一個變量,而不是一個字符串本身。 –

+0

有趣!很高興知道。對困惑感到抱歉。 – mikemike396

7

這是Bhupendra's answer雨燕轉換:

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") 
} 
1

這Bhupendra的答案的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") 
    } 
} 
相關問題