2011年9月21日 星期三

如何實作通知使用者有version update

可能有一種需求是我們希望如果appstore上有新版的軟體上架
我們會要通知使用者是否要更新
這時候我們可以透過這個URL去取得你的app資訊
http://itunes.apple.com/lookup?id=appid
回傳的結果可能是這樣.
中間就有版本號碼啦
不要問我怎麼抓怎麼parse json
這個solution很好找的
{
    "resultCount": 1,
    "results": [
        {
            "kind": "software",
            "artistId": 291728775,
            "artistName": "5Knot",
            "price": 0,
            "version": "1.1",
            "description": "90 Minutes .....",
            "genreIds": [
                "6005",
                "6012"
            ]
        }
    ]
}
然後自己local的版本可以這樣拿
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
自己再寫一個NSString的category去比較版本
@implementation NSString(VersionCompare)
- (NSComparisonResult)compareVersion:(NSString *)version
{
     NSArray *thisVersionParts = [self componentsSeparatedByString:@"."];
     NSArray *versionParts = [version componentsSeparatedByString:@"."];
     NSUInteger count = MIN([thisVersionParts count], [versionParts count]);
     NSUInteger i;
     for (i = 0; i < count; i++)
     {
          NSUInteger thisPart = [[thisVersionParts objectAtIndex:i] integerValue];
          NSUInteger part = [[versionParts objectAtIndex:i] integerValue];
          if (thisPart > part)
          {
               return NSOrderedDescending; //version is older
          }
          else if (thisPart < part)
          {
               return NSOrderedAscending; //version is newer
          }
     }
     if ([thisVersionParts count] > [versionParts count])
     {
          return NSOrderedDescending; //version is older
     }
     else if ([thisVersionParts count] < [versionParts count])
     {
          return NSOrderedAscending; //version is newer
     }
     return NSOrderedSame; //version is the same
}
搞定!!

Rerefenace
[1] Official search API
[2] iVersion


1 則留言: