IOS版本檢查更新 沒有用了

NSString *currentVersion = [self getLocalVersion];
        NSString *lastVersion = [self getCurrentAppStoreVersion];
        if (![lastVersion isEqualToString:currentVersion]) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新埠对,是否前往更新返劲?" delegate:self cancelButtonTitle:@"關(guān)閉" otherButtonTitles:@"更新", nil];
            alert.tag = 10000;
            [alert show];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本為最新版本" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            alert.tag = 10001;
            [alert show];
        }

//本機(jī)版本號(hào)

- (NSString *)getLocalVersion{
    
    NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"];
    return currentVersion;
}


//線上版本號(hào)
- (NSString *)getCurrentAppStoreVersion
{
    NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *infoArray = [dic objectForKey:@"results"];
    NSString *lastVersion = nil;
    if ([infoArray count]) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        lastVersion = [releaseInfo objectForKey:@"version"];
        NSLog(@"HAHAHAHA%@",lastVersion);
    }
    
    return lastVersion;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10000) {
        if (buttonIndex==1) {
            NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/us/app/id550383618?mt=8"];
            [[UIApplication sharedApplication]openURL:url];
        }
    }
}

當(dāng)前版本

    - (NSString *)getCurrentLocalVersion{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
    CFShow(infoDic);  
    NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"]; 
       }   
線上版本
       - (NSString *)getCurrentAppStoreVersion
        {
    NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *infoArray = [dic objectForKey:@"results"];
    NSString *lastVersion = nil;
    if ([infoArray count]) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        lastVersion = [releaseInfo objectForKey:@"version"];
        NSLog(@"HAHAHAHA%@",lastVersion);
    }
    
    return lastVersion;
    }

下面是解析

         - (NSString *)getCurrentAppStoreVersion
     {
    //        具體步驟如下:
    
        //        具體步驟如下:
        //        1皂冰,用 POST 方式發(fā)送請(qǐng)求:
        //    http://itunes.apple.com/search?term=你的應(yīng)用程序名稱&entity=software
        //
        //        更加精準(zhǔn)的做法是根據(jù) app 的 id 來查找:
        //    http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID
        //#define APP_URL http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID
        //
        //        你的應(yīng)用程序的ID 是 itunes connect里的 Apple ID
        //
        //        2拣技,從獲得的 response 數(shù)據(jù)中解析需要的數(shù)據(jù)。因?yàn)閺?appstore 查詢得到的信息是 JSON 格式的,所以需要經(jīng)過解析脏里。解析之后得到的原始數(shù)據(jù)就是如下這個(gè)樣子的:
        //        {
        //            resultCount = 1;
        //            results =     (
        //                           {
        //                               artistId = 開發(fā)者 ID;
        //                               artistName = 開發(fā)者名稱;
        //                               price = 0;
        //                               isGameCenterEnabled = 0;
        //                               kind = software;
        //                               languageCodesISO2A =             (
        //                                                                 EN
        //                                                                 );
        //                               trackCensoredName = 審查名稱;
        //                               trackContentRating = 評(píng)級(jí);
        //                               trackId = 應(yīng)用程序 ID;
        //                               trackName = 應(yīng)用程序名稱";
        //                               trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
        //                               userRatingCount = 用戶評(píng)級(jí);
        //                               userRatingCountForCurrentVersion = 1;
        //                               version = 版本號(hào);
        //                               wrapperType = software;
        //                           }
        //                           );
        //        }
        //
        //        然后從中取得 results 數(shù)組即可,具體代碼如下所示:
        //
        //        NSDictionary *jsonData = [dataPayload JSONValue];
        //        NSArray *infoArray = [jsonData objectForKey:@"results"];
        //        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        //        NSString *latestVersion = [releaseInfo objectForKey:@"version"];
        //        NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];
        //
        //        如果你拷貝 trackViewUrl 的實(shí)際地址虹曙,然后在瀏覽器中打開迫横,就會(huì)打開你的應(yīng)用程序在 appstore 中的介紹頁面。當(dāng)然我們也可以在代碼中調(diào)用 safari 來打開它酝碳。
        //        UIApplication *application = [UIApplication sharedApplication];
        //        [application openURL:[NSURL URLWithString:trackViewUrl]];
        //
        
    
    //        具體代碼如下:
    //{
                NSString *URL = @"http://itunes.apple.com/lookup?id=550383618";
                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setURL:[NSURL URLWithString:URL]];
                [request setHTTPMethod:@"POST"];
                NSHTTPURLResponse *urlResponse = nil;
                NSError *error = nil;
                NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
        
//                NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
//                NSDictionary *dic = [results JSONValue];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
    
    
                NSArray *infoArray = [dic objectForKey:@"results"];
                if ([infoArray count]) {
                    NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
                    NSString *lastVersion = [releaseInfo objectForKey:@"version"];
                    NSLog(@"HAHAHAHA%@",lastVersion);
                }
    //}
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];
    [[UIApplication sharedApplication]openURL:url];
    return @"1234";
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末矾踱,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子疏哗,更是在濱河造成了極大的恐慌呛讲,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件返奉,死亡現(xiàn)場(chǎng)離奇詭異贝搁,居然都是意外死亡垮媒,警方通過查閱死者的電腦和手機(jī)耐床,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粟矿,“玉大人污尉,你說我怎么就攤上這事膀哲。” “怎么了被碗?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵某宪,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我锐朴,道長(zhǎng)兴喂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任焚志,我火速辦了婚禮衣迷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘娩嚼。我一直安慰自己蘑险,他們只是感情好滴肿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布岳悟。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贵少。 梳的紋絲不亂的頭發(fā)上呵俏,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音滔灶,去河邊找鬼普碎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛录平,可吹牛的內(nèi)容都是我干的麻车。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼斗这,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼动猬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起表箭,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤赁咙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后免钻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體彼水,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年极舔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凤覆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡姆怪,死狀恐怖叛赚,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情稽揭,我是刑警寧澤俺附,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站溪掀,受9級(jí)特大地震影響事镣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜揪胃,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一璃哟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧喊递,春花似錦随闪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撮奏。三九已至,卻和暖如春当宴,著一層夾襖步出監(jiān)牢的瞬間畜吊,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工户矢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留玲献,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓梯浪,卻偏偏與公主長(zhǎng)得像捌年,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子挂洛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容