アプリが最初にインストールされたときにのみ表示されるビューを更新後に再び表示する必要があるため、アプリが更新されているかどうかをいつ起動するかを確認する必要があります。
8 に答える
値 (現在のアプリのバージョン番号など) を保存してNSUserDefaults
、ユーザーがアプリを起動するたびに確認することができます。
- (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;
}
swift-2バージョンは次のようになります。
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()
}
swift-3バージョンは次のようになります。
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()
}
アプリのバージョン番号を NSUserDefaults に保存し、アプリを起動するたびに確認できます。番号が利用できない場合は、新規インストールです。変更された場合、それはアップグレードです。
小規模なアプリケーションの場合は与えられた答えは良いと思いますが、長いロードマップを持つ大規模な iOS アプリに取り組んでいるときは、次のような利点を持つ強力で未来的なソリューションが間違いなく必要です。
- すべての可能性 (2.0/2.0.0 など) で正常に機能するロジックをより適切に比較します。
- ユーザーがバージョン 1.0 から 2.0 へ、または 1.1 から 2.0 へ移行する場合を想定して、移行中に特定のアクションを実行します。
- ユーザーがアプリをリセット (ログアウト) したときに、キャッシュされたバージョンをリセットします。
以下は、iOS アプリの 1 つで使用したコード スニペットです。
public enum VersionConfigResponse {
case freshInstalled
case versionGreater
case versionEqualOrLesser
case currentVersionEmpty
}
/*
Config manager responsible to handle the change needs to be done when user migrate from one Version to another Version.
*/
public class VersionConfig {
public static let shared = VersionConfig()
private init() { }
private let versionKey = "SAVED_SHORT_VERSION_STRING"
/*
Cache the existing version for compare during next app launch.
*/
private var storedVersion : String? {
get {
return UserDefaults.standard.object(forKey: versionKey) as? String
} set {
UserDefaults.standard.set(newValue, forKey: versionKey)
UserDefaults.standard.synchronize()
}
}
/*
Validate the current version with saved version, if greater do clean.
*/
public func validate(currentVersion: String?) -> VersionConfigResponse {
guard let currentVersion = currentVersion else {
return .currentVersionEmpty
}
guard let sVersion = storedVersion else {
self.storedVersion = currentVersion
self.freshInstalled()
return .freshInstalled
}
if currentVersion.compare(sVersion, options: .numeric, range: nil, locale: nil) == .orderedDescending {
self.storedVersion = currentVersion
self.userMigrated(fromVersion: sVersion, toVersion:currentVersion)
return .versionGreater
} else {
return .versionEqualOrLesser
}
}
private func userMigrated(fromVersion: String, toVersion: String) {
//take your action here...
}
private func freshInstalled() {
//take your action here...
}
/*
Remove saved version if user reset(logout) the app.
*/
public func reset() {
self.storedVersion = nil
}
}
//trigger version config with current version
VersionConfig.shared.validate(currentVersion: "1.0.0")
//reset when user logsout
VersionConfig.shared.reset()
BundleInfoVersioning
この問題の解決に役立つ迅速なパッケージを作成しました。
CFBundleVersion
やなどの情報ディクショナリ内のすべてのキーと、CFBundleShortVersionString
たとえば のようなカスタム キー パスで機能しますNSAgora/DatabaseVersion
。
例:
let bundleInfoVersioning = BundleInfoVersioning(bundle: .main)
bundleInfoVersioning.check(forKeyPath: "CFBundleVersion") { (old: String?, new: String?) in
if old == nil {
Analytics.install(version: new)
}
else {
Analytics.update(from: old, to: new)
}
}
bundleInfoVersioning.check(forKeyPath: "CFBundleShortVersionString") { _ , newVersion in
self.showWhatsNew(in: newVersion)
}
bundleInfoVersioning.check(forKeyPath: "NSAgora/DatabaseVersion") { (old: Int?, new: Int?) in
self.resetDataBase()
}
https://github.com/nsagora/bundle-info-versioningの github で確認できます。