iOS自定義版本更新檢查

1、效果圖

提示新版本
提示新版本(讀取了AppStore寫的更新方案)

2、關鍵點

請求http://itunes.apple.com/lookup?id=968615456,就會返回關于這個應用的詳細內容的JSON數據,后面就簡單了吧

我的應用返回的JSON數據(已經格式化):

{
    "resultCount": 1,
    "results": [
        {
            "advisories": [
                "Unrestricted Web Access"
            ],
            "artistId": 914453386,
            "artistName": "he tiancong",
            "artistViewUrl": "https://itunes.apple.com/us/artist/he-tiancong/id914453386?uo=4",
            "artworkUrl100": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
            "artworkUrl512": "http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/6f/89/92/6f8992e4-5c10-be87-9242-d933ddb0780a/mzl.jxbacfmg.png",
            "artworkUrl60": "http://is3.mzstatic.com/image/pf/us/r30/Purple3/v4/2e/cc/a9/2ecca990-b82a-a238-793a-a16b59b56389/AppIcon60x60_U00402x.png",
            "bundleId": "CampusOfGLUT",
            "contentAdvisoryRating": "17+",
            "currency": "USD",
            "description": "桂林理工大學——校園通正式上線!

應用提供:
1、學校新聞、通知等動態;
2、學校學院、學習等、
3、學習 、生活等多欄目,分享和交流平臺~

歡迎大家提供建議和評價!

郵件:ihetiancong@qq.com
QQ:3027201361",
            "features": [
                "iosUniversal"
            ],
            "fileSizeBytes": "6436734",
            "formattedPrice": "Free",
            "genreIds": [
                "6017",
                "6012"
            ],
            "genres": [
                "Education",
                "Lifestyle"
            ],
            "ipadScreenshotUrls": [
                "http://a1.mzstatic.com/us/r30/Purple3/v4/cf/36/c1/cf36c120-3798-303d-6ae6-cb70da8c7b18/screen480x480.jpeg",
                "http://a3.mzstatic.com/us/r30/Purple3/v4/4c/34/19/4c3419c4-c777-f202-0f5a-a1e733bf37a8/screen480x480.jpeg",
                "http://a2.mzstatic.com/us/r30/Purple5/v4/9e/9b/35/9e9b3592-4e93-148b-0fd8-73d8ed389388/screen480x480.jpeg"
            ],
            "isGameCenterEnabled": false,
            "kind": "software",
            "languageCodesISO2A": [
                "EN",
                "FR",
                "DE",
                "JA",
                "KO",
                "PL",
                "RU",
                "ZH",
                "ES",
                "ZH",
                "UK"
            ],
            "minimumOsVersion": "8.1",
            "price": 0.0,
            "primaryGenreId": 6017,
            "primaryGenreName": "Education",
            "releaseDate": "2015-03-18T09:34:28Z",
            "releaseNotes": "——————那些年,最后一次更新的版本——————

1、優化iPhone和iPad下顯示效果
2、增加話題圖標,內容更加醒目
3、增加一些可有可無的更新

——————那些年,我們一起說過的話題——————

1、非常感謝那些年i被拒絕的女孩,她們讓我充實成長
2、非常感謝那些年i被關照的老師,他們讓我無憂學習
3、非常感謝那些年i被信任的同學,他們讓我珍惜想念
4、非常感謝那些年i被堅持的自己,你們讓我相信自己

——————最后說明——————

1、應用為i畢業設計作品,不提供商業信息,感謝大家支持
2、應用中的學校新聞只提參考,由于解析原因,可能個別新聞無法顯示,真實動態請瀏覽(www.glut.edu.cn)
3、校園話題的意義是希望能將學校的信息通過移動互聯網重新整合在一起,這是一個信息未來的趨勢
4、其它問題請聯系:ihetiancong@qq.com

——————謝謝大家——————",
            "screenshotUrls": [
                "http://a2.mzstatic.com/us/r30/Purple1/v4/1f/bf/32/1fbf321a-c22b-f938-0d84-17e65730e7a9/screen1136x1136.jpeg",
                "http://a5.mzstatic.com/us/r30/Purple3/v4/c9/92/ec/c992ec71-7576-6334-3ebb-4aa857880219/screen1136x1136.jpeg",
                "http://a1.mzstatic.com/us/r30/Purple5/v4/72/27/ae/7227aed1-62fa-6946-e63e-52090ce6a021/screen1136x1136.jpeg",
                "http://a4.mzstatic.com/us/r30/Purple2/v4/13/46/d3/1346d396-4126-ab48-ae15-b0038b92bd68/screen1136x1136.jpeg"
            ],
            "sellerName": "he tiancong",
            "sellerUrl": "http://weibo.com/iHTCapp",
            "supportedDevices": [
                "iPhone5c",
                "iPhone6Plus",
                "iPodTouchFifthGen",
                "iPadThirdGen4G",
                "iPad2Wifi",
                "iPadFourthGen",
                "iPhone5s",
                "iPhone4S",
                "iPadThirdGen",
                "iPad23G",
                "iPhone6",
                "iPadFourthGen4G",
                "iPhone5",
                "iPadMini",
                "iPadMini4G"
            ],
            "trackCensoredName": "桂林理工大學---校園通",
            "trackContentRating": "17+",
            "trackId": 968615456,
            "trackName": "桂林理工大學---校園通",
            "trackViewUrl": "https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4",
            "version": "1.3",
            "wrapperType": "software"
        }
    ]
}

3、檢查更新Demo

  • 檢查更新
[AFNetworkingUtils getRequestWithUrl:@"http://itunes.apple.com/lookup?id=968615456" afterExecute:^(NSDictionary *resultDic) {
 
            float version =[[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] floatValue];
            NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
            float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];

            if(version>currentVersion){
                NSString *alertTitle=[@"桂林理工大學-校園通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",version]];
                NSString *alertMsg=@"是否要更新?";
               //NSString *alertMsg  = [[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"]
                UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:alertTitle message:alertMsg delegate:self cancelButtonTitle:@"稍后更新" otherButtonTitles:@"立即更新", nil];
                [alertView show];
            }
    } onError:^{
    }];
  • 跳到AppStore更新
        NSString *url = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

4、自定義成一個工具

  • 使用時調用這個方法就行
#pragma mark - 檢查版本更新
-(void) checkUpdate{
    
    InformationHandleTool * tool = [InformationHandleTool sharedInfoTool];
    [tool checkUpdateWithAppID:@"968615456" success:^(NSDictionary *resultDic, BOOL isNewVersion, NSString *newVersion) {
        
        if (isNewVersion) {
            [self showUpdateView:newVersion];
        }
        
    } failure:^(NSError *error) {
        ;
    }];

}
  • 網絡請求方法-(void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
#pragma mark - 檢查版本更新
\- (void)checkUpdateWithAppID:(NSString *)appID success:(void (^)(NSDictionary *resultDic , BOOL isNewVersion , NSString * newVersion))success failure:(void (^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString *encodingUrl=[[@"http://itunes.apple.com/lookup?id=" stringByAppendingString:appID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:encodingUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
         NSString * versionStr =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
        
        float version =[versionStr floatValue];
        //self.iTunesLink=[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"trackViewUrl"];
        NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
        float currentVersion = [[infoDic valueForKey:@"CFBundleShortVersionString"] floatValue];
        
        if(version>currentVersion){
            
            success(resultDic, YES, versionStr);
            
        }else{
            
            success(resultDic,NO ,versionStr);
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    }];
}
  • 提醒方法- (void)showUpdateView:(NSString *)newVersion
    ** 注:這個是iOS8以后的UIAlertController **
\- (void)showUpdateView:(NSString *)newVersion
{
    NSString *alertMsg=[[@"桂林理工大學-校園通v" stringByAppendingString:[NSString stringWithFormat:@"%0.1f",[newVersion floatValue]]] stringByAppendingString:@",趕快體驗最新版本吧!"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"發現新版本" message:alertMsg preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按鈕
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSString *str = @"https://itunes.apple.com/us/app/gui-lin-li-gong-da-xue-xiao/id968615456?mt=8&uo=4";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
    }]];

    [self presentViewController:alert animated:YES completion:nil];
}

5、檢查增強

  • 服務器版本跟本地版本可以隨意,比如服務器1.4.1 ,本地1.4,也能檢查了。

            NSString * serverVersion =[[[resultDic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
            NSString * localVersion = [SystemUtils getVersionSting];
            
            //以"."分隔數字然后分配到不同數組
            NSArray * serverArray = [serverVersion componentsSeparatedByString:@"."];
            NSArray * localArray = [localVersion componentsSeparatedByString:@"."];
            
            for (int i = 0; i < serverArray.count; i++) {
                
                //以服務器版本為基準,判斷本地版本位數小于服務器版本時,直接返回(并且判斷為新版本,比如服務器1.5.1 本地為1.5)
                if(i > (localArray.count -1)){
                   //有新版本,提示!
                     break;
                }
                
                //有新版本,服務器版本對應數字大于本地
                if ( [serverArray[i] intValue] > [localArray[i] intValue]) {
                     //有新版本,提示!
                    break;
                }
            }
  • 如果有什么疑問,可以在評論區一起討論;
  • 如果有什么不正確的地方,歡迎指導!


注:本文首發于 iHTCboy's blog,如若轉載,請注明來源。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,517評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,087評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,521評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,493評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,207評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,603評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,624評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,813評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,364評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,110評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,305評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,874評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,532評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,953評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,209評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,033評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,268評論 2 375

推薦閱讀更多精彩內容