2013-06-03 6 views
15

Muszę sprawdzić, kiedy moja aplikacja zostanie uruchomiona, jeśli była aktualizowana, ponieważ potrzebuję , aby utworzyć widok, który pojawia się tylko wtedy, gdy aplikacja jest instalowana po raz pierwszy i będzie ponownie wyświetlana po zaktualizowaniu .Sprawdź, czy moja aplikacja IOS jest zaktualizowana

+2

Musisz opracować na to pytanie: co jest aktualizowane? sama aplikacja lub pliki w aplikacji lub pliki do pobrania ze zdalnego serwera? –

+0

sama aplikacja jest aktualizowana – user2412870

Odpowiedz

34

Możesz zapisać wartość (np. Bieżący numer wersji aplikacji) na NSUserDefaults i sprawdzać ją za każdym razem, gdy użytkownik uruchomi aplikację.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // ... 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSString *currentAppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; 
    NSString *previousVersion = [defaults objectForKey:@"appVersion"]; 
    if (!previousVersion) { 
     // first launch 

     // ... 

     [defaults setObject:currentAppVersion forKey:@"appVersion"]; 
     [defaults synchronize]; 
    } else if ([previousVersion isEqualToString:currentAppVersion]) { 
     // same version 
    } else { 
     // other version 

     // ... 

     [defaults setObject:currentAppVersion forKey:@"appVersion"]; 
     [defaults synchronize]; 
    } 



    return YES; 
} 

Wersja wygląda następująco:

let defaults = NSUserDefaults.standardUserDefaults() 

let currentAppVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String 
let previousVersion = defaults.stringForKey("appVersion") 
if previousVersion == nil { 
    // first launch 
    defaults.setObject(currentAppVersion, forKey: "appVersion") 
    defaults.synchronize() 
} else if previousVersion == currentAppVersion { 
    // same version 
} else { 
    // other version 
    defaults.setObject(currentAppVersion, forKey: "appVersion") 
    defaults.synchronize() 
} 

Wersja wygląda następująco:

let defaults = UserDefaults.standard 

let currentAppVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String 
let previousVersion = defaults.string(forKey: "appVersion") 
if previousVersion == nil { 
    // first launch 
    defaults.set(currentAppVersion, forKey: "appVersion") 
    defaults.synchronize() 
} else if previousVersion == currentAppVersion { 
    // same version 
} else { 
    // other version 
    defaults.set(currentAppVersion, forKey: "appVersion") 
    defaults.synchronize() 
} 
+3

, w jaki sposób sprawdzamy czy już dostarczona aplikacja nie obsługuje powyższego mechanizmu? – damithH

+3

@damithH jeśli wysyłają aplikacja używała NSUserDefaults, a potem po prostu sprawdzić istnienie niezależnie od klucz ty używasz. –

6

możesz przechowywać numer wersji aplikacji w NSUserDefaults i sprawdzać go przy każdym uruchomieniu aplikacji. Jeśli numer nie jest dostępny, jest to świeża instalacja. Jeśli zostanie zmieniony, jest to aktualizacja.

+0

Przeczytałem, że po aktualizacji NSUserDefaults nie zostanie usunięty, więc to by mi nie pomogło, działałoby, gdyby użytkownik najpierw usunął aplikację i ponownie ją zainstalował. – user2412870

+1

@ user2412870 Przechowuj swoje numer wersji w NSUserDefaults i sprawdź, czy po aktualizacji możesz sprawdzić, czy numer wersji jest podobny do numeru zapisanego w NSUserDefaults – pre

3

wersja z ważnym poprawy nad przyjętej odpowiedź:

  • użyciu infoDictionary zamiast objectForInfoDictionaryKey gwarancje, że wynik jest niezależny od języka urządzenia, w przeciwnym wypadku może skończyć się w niektórych rzadkich przypadkach, wierząc, że jest to uaktualnienie, gdy w rzeczywistości jest to tylko zmiana języka urządzenie
  • stosując klucz identyczny do UserDefaults do głównego Bundle infoDictionary dla jasności co jest dokładnie zapisane
  • faktoring kod ustawienia CurrentVersion składnia
  • Swift 3

Kod:

let standardUserDefaults = UserDefaults.standard 
    let shortVersionKey = "CFBundleShortVersionString" 
    let currentVersion = Bundle.main.infoDictionary![shortVersionKey] as! String 
    let previousVersion = standardUserDefaults.object(forKey: shortVersionKey) as? String 
    if previousVersion == currentVersion { 
     // same version 
    } else { 
     // replace with `if let previousVersion = previousVersion {` if you need the exact value 
     if previousVersion != nil { 
      // new version 
     } else { 
      // first launch 
     } 
     standardUserDefaults.set(currentVersion, forKey: shortVersionKey) 
     standardUserDefaults.synchronize() 
    } 
0

Swift 3.x

Wystarczy zainicjować AppVersionUpdateNotifier w uruchomieniu aplikacji i zgodne AppUpdateNotifier protokół cieszyć.

class AppVersionUpdateNotifier { 
    static let KEY_APP_VERSION = "key_app_version" 
    static let shared = AppVersionUpdateNotifier() 

    private let userDefault:UserDefaults 
    private var delegate:AppUpdateNotifier? 

    private init() { 
     self.userDefault = UserDefaults.standard 
    } 

    func initNotifier(_ delegate:AppUpdateNotifier) { 
     self.delegate = delegate 
     checkVersionAndnotify() 
    } 

    private func checkVersionAndnotify() { 
     let versionOfLastRun = userDefault.object(forKey: AppVersionUpdateNotifier.KEY_APP_VERSION) as? Int 
     let currentVersion = Int(Bundle.main.buildVersion)! 

     if versionOfLastRun == nil { 
      // First start after installing the app 

     } else if versionOfLastRun != currentVersion { 
      // App was updated since last run 
      delegate?.onVersionUpdate(newVersion: currentVersion, oldVersion: versionOfLastRun!) 
     } else { 
      // nothing changed 

     } 
     userDefault.set(currentVersion, forKey: AppVersionUpdateNotifier.KEY_APP_VERSION) 
    } 
} 

protocol AppUpdateNotifier { 
    func onFirstLaunch() 
    func onVersionUpdate(newVersion:Int, oldVersion:Int) 
} 
extension Bundle { 
    var shortVersion: String { 
     return infoDictionary!["CFBundleShortVersionString"] as! String 
    } 
    var buildVersion: String { 
     return infoDictionary!["CFBundleVersion"] as! String 
    } 
} 
//************************* 
//Use: Example 
//************************* 

extention AppDelagate: AppUpdateNotifier { 
    func onVersionUpdate(newVersion: Int, oldVersion: Int) { 
     // do something 
    } 

    func onFirstLaunch() { 
     //do something 
    } 
} 
0

Oto prosty kod wiedzieć, czy obecna wersja jest inna

-(BOOL) needsUpdate 
{ 
    NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary]; 
    NSString* appID = infoDictionary[@"CFBundleIdentifier"]; 
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]]; 
    NSData* data = [NSData dataWithContentsOfURL:url]; 
    NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

    if ([lookup[@"resultCount"] integerValue] == 1) 
    { 
     NSString* appStoreVersion = lookup[@"results"][0][@"version"]; 
     NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"]; 
     if (![appStoreVersion isEqualToString:currentVersion]) 
     { 
      NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion); 
      return YES; 
     } 
    } 
    return NO; 
} 

Note (ta praca kod na symulatorze zbyt.): Upewnij się, że po wprowadzeniu nowej wersji programu iTunes, to pasuje do wersji w wydanej aplikacji. Jeśli nie, powyższy kod zawsze zwróci TAK, niezależnie od tego, czy użytkownik zaktualizuje.