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
Odpowiedz
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 swift-2 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 swift-3 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()
}
, w jaki sposób sprawdzamy czy już dostarczona aplikacja nie obsługuje powyższego mechanizmu? – damithH
@damithH jeśli wysyłają aplikacja używała NSUserDefaults, a potem po prostu sprawdzić istnienie niezależnie od klucz ty używasz. –
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.
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
@ 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
swift wersja z ważnym poprawy nad przyjętej odpowiedź:
- użyciu
infoDictionary
zamiastobjectForInfoDictionaryKey
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()
}
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
}
}
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.
Musisz opracować na to pytanie: co jest aktualizowane? sama aplikacja lub pliki w aplikacji lub pliki do pobrania ze zdalnego serwera? –
sama aplikacja jest aktualizowana – user2412870