IOS判斷app在appstore是否有可用的更新

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   使用   sp   for   

iTunes可以提供app的版本資訊,主要通過appid擷取,如 http://itunes.apple.com/lookup?id=946449501,使用時只需要到iTunes尋找自己的appid,修改成自己的appid即可

使用HTTP模式讀取此連結可以擷取app資訊的json字串

貼出部分代碼

-(void)checkVersion{    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];//strURL為你的appid地址    [request setRequestMethod:@"POST"];    [request setDelegate:self];    [request startAsynchronous];}-(void)requestFinished:(ASIHTTPRequest *)request{    NSString *recStr = [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding];    recStr = [recStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//返回的字串有前面有很多分行符號,需要去除一下    NSDictionary *resultDic = [JSONHelper DeserializerDictionary:recStr];//jsonhelper是我封裝的json解析類,你可以使用自己方式解析        NSArray *infoArray = [resultDic objectForKey:@"results"];    if (infoArray.count > 0) {                NSDictionary* releaseInfo =[infoArray objectAtIndex:0];        NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];                NSArray *curVerArr = [currentVersion componentsSeparatedByString:@"."];        NSArray *appstoreVerArr = [appStoreVersion componentsSeparatedByString:@"."];        BOOL needUpdate = NO;        //比較版本號碼大小        int maxv = (int)MAX(curVerArr.count, appstoreVerArr.count);        int cver = 0;        int aver = 0;        for (int i = 0; i < maxv; i++) {            if (appstoreVerArr.count > i) {                aver = [NSString stringWithFormat:@"%@",appstoreVerArr[i]].intValue;            }            else{                aver = 0;            }            if (curVerArr.count > i) {                cver = [NSString stringWithFormat:@"%@",curVerArr[i]].intValue;            }            else{                cver = 0;            }            if (aver > cver) {                needUpdate = YES;                break;            }        }                //如果有可用的更新        if (needUpdate){                        trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];//trackViewURL臨時變數儲存app,可以讓app跳轉到appstore            UIAlertView* alertview =[[UIAlertView alloc] initWithTitle:@"版本升級" message:[NSString stringWithFormat:@"發現有新版本,是否升級?"] delegate:self cancelButtonTitle:@"暫不升級" otherButtonTitles:@"馬上升級", nil];            [alertview show];                    }            }}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 1){        UIApplication *application = [UIApplication sharedApplication];        [application openURL:[NSURL URLWithString:trackViewURL]];    }}

 

IOS判斷app在appstore是否有可用的更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.