中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

iOS 檢測(cè)版本更新

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

如果我們要檢測(cè)app版本的更新,那么我們必須獲取當(dāng)前運(yùn)行app版本的版本信息和appstore 上發(fā)布的最新版本的信息。


當(dāng)前運(yùn)行版本信息可以通過(guò)info.plist文件中的 version中獲。

 NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

 NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];


這樣就獲取到當(dāng)前運(yùn)行的app的版本了


具體步驟如下:
1,用 POST 方式發(fā)送請(qǐng)求:

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


   NSString *URL = @"http://itunes.apple.com/lookup";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];

    // ? 數(shù)據(jù)體

    NSString *str = [NSString stringWithFormat:@"id=你的應(yīng)用程序的ID"];

    // 將字符串轉(zhuǎn)換成數(shù)據(jù)

    request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

    // 開(kāi)始工作,在很多多線程技術(shù)中,start run

    dispatch_async(dispatch_queue_create("demo", DISPATCH_QUEUE_CONCURRENT), ^{

        [connection start];

    });

      NSURLConnection 代理中獲取數(shù)據(jù)處理數(shù)據(jù)-這里不多說(shuō)了。。


2,從獲得的 response 數(shù)據(jù)中解析需要的數(shù)據(jù)。因?yàn)閺?appstore 查詢得到的信息是 JSON 格式的,所以需要經(jīng)過(guò)解析。解析之后得到的原始數(shù)據(jù)就是如下這個(gè)樣子的:
{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 開(kāi)發(fā)者 ID;  
            artistName = 開(kāi)發(fā)者名稱(chēng); 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 審查名稱(chēng);  
            trackContentRating = 評(píng)級(jí);  
            trackId = 應(yīng)用程序 ID;  
            trackName = 應(yīng)用程序名稱(chē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í)際地址,然后在瀏覽器中打開(kāi),就會(huì)打開(kāi)你的應(yīng)用程序在 appstore 中的介紹頁(yè)面。當(dāng)然我們也可以在代碼中調(diào)用 safari 來(lái)打開(kāi)它。
UIApplication *application = [UIApplication sharedApplication];  
[application openURL:[NSURL URLWithString:trackViewUrl]];  


代碼如下:

-(void)onCheckVersion

{

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];

    NSString *URL = @"http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:URL]];

    [request setHTTPMethod:@"POST"];

    NSHTTPURLResponse *urlResponse = nil;

    NSError *error = nil;

    NSData *recervedData = [NSURLConnection sendSynchronousRequest:requestreturningResponse:&urlResponse error:&error]; 

    NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedDatalength] encoding:NSUTF8StringEncoding];

    NSDictionary *dic = [results JSONValue];

    NSArray *infoArray = [dic objectForKey:@"results"];

    if ([infoArray count]) {

        NSDictionary *releaseInfo = [infoArray objectAtIndex:];

        NSString *lastVersion = [releaseInfo objectForKey:@"version"];

        

        if (![lastVersion isEqualToString:currentVersion]) {

            //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];

            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];

        }

    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (alertView.tag==10000) {

        if (buttonIndex==1) {

            NSURL *url = [NSURL URLWithString:@"軟件在appstore中的鏈接"];

            [[UIApplication sharedApplication]openURL:url];

        }

    }

標(biāo)簽: isp 代碼 開(kāi)發(fā)者

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php 服務(wù)器限速代碼

下一篇:Android判斷網(wǎng)絡(luò)狀態(tài)方法