天天看點

iOS 版本檢測更新

#pragma mark - 檢查更新
- (void)checkVersion
{
    if ([AppID length] < ) {
        return;
    }

    NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppID];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] init];
    [urlRequest setURL:[NSURL URLWithString:url]];
    [urlRequest setHTTPMethod:@"POST"];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
    NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:returnData options: error:nil];

    NSArray *infoArr = [jsonDic objectForKey:@"results"];
    if ([infoArr count]) {
        NSDictionary *releaseInfo = [infoArr objectAtIndex:];
        NSString *appStoreVersion = [releaseInfo objectForKey:@"version"];

        // 這裡顯示的是appstore 上面設定的号碼.
        NSLog(@"appstore 版本為:%@ \n 目前版本為:%@",appStoreVersion,VERSION);

        if ([VERSION compare: appStoreVersion] == NSOrderedAscending) {
            // 彈窗
            NSArray *items = @[MMItemMake(@"立即更新", MMItemTypeHighlight, ^(NSInteger index) {
                NSURL *url = [NSURL URLWithString:DownloadUrl];
                NSLog(@"跳轉下載下傳位址 : %@", DownloadUrl);
                [[UIApplication sharedApplication] openURL:url];
            }),MMItemMake(@"稍後提醒", MMItemTypeNormal, nil)];

            [[[MMAlertView alloc] initWithTitle:@"提示" detail:@"出新版本啦,快點更新吧!" items:items]showWithBlock:nil];

        }
    }
}