2012-01-06 109 views
8

我目前在應用商店中有一個應用程序,我打算很快提交更新。如何判斷iOS應用程序是否已新安裝或更新?

有了這個更新我要添加代碼,這將告訴應用程序時,第一次運行application:didFinishLaunchingWithOptions不管是:從App Store

  1. 一個新的安裝。
  2. 從以前的版本

最新更新有一個在應用程序沒有代碼目前在App Store來處理這個問題。
該應用程序使用SQLite數據庫,但由於我不會進入這裏的原因,我不想使用檢查它的存在作爲解決此問題的方法。

作爲一個方面的問題,而無需手動存儲數據,是否有一個SDK我可以使用當一個應用程序被安裝到一個裝置來查詢? (最好的iOS 3.0兼容)

我已經看到了similar question,但沒有答案的應用與現有的應用程序商店代碼工作。

回答

7

您可以將版本號保存到NSUserDefaults,並相應地更新它。

如果這樣做不起作用,您可以發佈介紹版本控制方案的中間版本。

如果這不是一個選項,您可以檢查從您創建的文件,或者你設置有條件或懶洋洋地喜好以前運行的痕跡。

+4

那麼問題是什麼?沒有版本號==舊版本。 – Krizz 2012-01-06 09:37:02

+0

在某些情況下,您可以在該場景中的中間版本中引入版本方案。你能想到第一次啓動時不存在的任何持久性數據或首選項嗎? – justin 2012-01-06 09:38:41

+0

酷 - 我已經擴大了與這些細節的答案。 – justin 2012-01-06 09:47:21

18

以下代碼可能有助於回答您關於何時安裝應用程序的問題。我不確定應用程序包創建日期是XCode生成日期還是下載日期,因爲它未經過應用商店測試。

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; // e.g. /var/mobile/Applications/<GUID>/<AppName>.app 
NSFileManager *manager = [NSFileManager defaultManager]; 
NSDictionary* attrs = [manager attributesOfItemAtPath:bundleRoot error:nil]; 
NSLog(@"Build or download Date/Time of first version to be installed: %@", [attrs fileCreationDate]); 
NSLog(@"Date/Time of last install (unless bundle changed by code): %@", [attrs fileModificationDate]); 
NSString *rootPath = [bundleRoot substringToIndex:[bundleRoot rangeOfString:@"/" options:NSBackwardsSearch].location]; // e.g /var/mobile/Applications/<GUID> 
attrs = [manager attributesOfItemAtPath:rootPath error:nil]; 
NSLog(@"Date/Time first installed (or first reinstalled after deletion): %@", [attrs fileCreationDate]); 
+0

感謝這種新的方法來查找是否有新的安裝,即使我們在以前的版本中沒有NSUserDefaults設置。 – Alex 2013-03-15 12:19:49

+2

我使用現有的appstore應用程序進行測試,看起來第一個日誌給出了下載日期/時間而不是構建日期/時間。 – Alex 2013-03-15 12:38:24

+2

這是一個非常好的答案!應該正確地標記正確的答案。 – 2014-02-20 12:50:29

3

試試這個代碼,我知道我爲這個答案太晚了,但是爲了knwoldge分享我在這裏。

-(void) checkIsAppUpdated 
{ 
NSString *urlString = @"http://itunes.apple.com/lookup?id=995558215"; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

[request setHTTPMethod:@"GET"]; 

NSError *error = nil; 
NSURLResponse *response = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

//NSString *stringReply = (NSString *)[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

if (error) 
{ 
    // NSLog(@"Error: %@", stringReply); 
    //error reviced 

} 
else 
{ 
    //The response is in data 
    //NSLog(@"Success: %@", stringReply); 
    NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
    float appStoreVersion=[[[[dictResponse objectForKey:@"results"] firstObject] objectForKey:@"version"] floatValue]; 

    NSLog(@"app stroe version=%f",appStoreVersion); 

    NSString *strLocalVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; 
    float localAppVersion=[strLocalVersion floatValue]; 
    if (localAppVersion!=appStoreVersion) 
    { 
     //open app store url 
     // NSString *iTunesLink = @"itms://itunes.apple.com/us/app/apple-store/id375380948?mt=8"; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/app/washthenfold/id995558215?mt=8"]]; 
    } 
} 

} 

ID:-replace ID與自己的應用程序ID。你可以從iTune的應用程序ID選擇連接APP->更多信息 - >元數據

因爲模擬器沒有應用程序商店的應用程序,此代碼將不能在模擬器工作。

0

GBVersionTracking is good pod追蹤所有版本的歷史。

[GBVersionTracking isFirstLaunchEver]; 
0

下面是一個簡單的代碼來了解當前的版本是不同的

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

注(在模擬器此代碼的工作了。):確保當你在iTunes中輸入新的版本,這與您正在發佈的應用中的版本相匹配。如果沒有,那麼無論用戶是否更新,上面的代碼總是會返回YES。

相關問題