2013-06-03 25 views
15

Tôi cần kiểm tra thời điểm ứng dụng của tôi khởi chạy nếu ứng dụng đã được cập nhật, vì tôi cần để tạo chế độ xem chỉ xuất hiện khi ứng dụng được cài đặt lần đầu xuất hiện lại sau khi được cập nhật .Kiểm tra xem ứng dụng IOS của tôi có được cập nhật

+2

bạn cần phải giải thích về câu hỏi này: những gì đang được cập nhật? bản thân ứng dụng hoặc tệp trong ứng dụng hoặc tệp sẽ được tải xuống từ máy chủ từ xa? –

+0

bản thân ứng dụng đang được cập nhật – user2412870

Trả lời

34

Bạn có thể lưu giá trị (ví dụ: số phiên bản ứng dụng hiện tại) thành NSUserDefaults và kiểm tra nó mỗi khi người dùng khởi động ứng dụng.

- (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; 
} 

Phiên bản trông như thế này:

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() 
} 

Phiên bản trông như thế này:

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

cách chúng tôi kiểm tra xem ứng dụng đã được giao không hỗ trợ cơ chế trên không? – damithH

+3

@damithH nếu ứng dụng được giao của bạn đang sử dụng NSUserDefaults, sau đó chỉ cần kiểm tra sự tồn tại của bất kỳ khóa nào bạn đang sử dụng. –

6

bạn có thể lưu số phiên bản ứng dụng trong NSUserDefaults và kiểm tra nó mỗi khi ứng dụng của bạn được khởi chạy. Nếu số không có sẵn, đó là một cài đặt mới. Nếu nó được thay đổi, nó là một bản nâng cấp.

+0

Tôi đọc sau khi cập nhật NSUserDefaults không bị xóa, vì vậy sẽ không giúp tôi, điều đó sẽ hoạt động nếu người dùng xóa ứng dụng trước và cài đặt lại ứng dụng – user2412870

+1

@ user2412870 số phiên bản trong NSUserDefaults và kiểm tra, nếu cập nhật, bạn có thể kiểm tra xem số phiên bản có tương tự như số được lưu trong NSUserDefaults – pre

3

phiên bản với một sự cải tiến quan trọng so với câu trả lời được chấp nhận:

  • sử dụng infoDictionary thay vì objectForInfoDictionaryKey bảo đảm kết quả độc lập với ngôn ngữ thiết bị, nếu không bạn có thể kết thúc trong một số trường hợp hiếm hoi tin rằng có nâng cấp khi thực tế nó chỉ là thay đổi ngôn ngữ thiết bị
  • bằng cách sử dụng khóa UserDefaults giống hệt nhau đến Bundle infoDictionary chính cho rõ ràng về những gì được lưu trữ một cách chính xác
  • bao thanh toán đang thiết lập CurrentVersion
  • Swift 3 cú pháp

Code:

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

Chỉ cần khởi AppVersionUpdateNotifier trong mắt ứng dụng và phù hợp với AppUpdateNotifier giao thức, thưởng thức.

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

Đây là một mã đơn giản để biết nếu phiên bản hiện tại là khác nhau

-(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; 
} 

Lưu ý (điều này làm việc mã trên mô phỏng quá.): Hãy chắc chắn rằng khi bạn nhập phiên bản mới trong iTunes, điều này khớp với phiên bản trong ứng dụng bạn đang phát hành. Nếu không thì mã trên sẽ luôn trả về YES bất kể người dùng có cập nhật hay không.

Các vấn đề liên quan