網(wǎng)上很多ios版本更新的帖子,但是很多都不用吟温,今天有個(gè)人問我這塊的問題序仙,我就把我之前寫的給他了, 其實(shí)道理很簡單就是拿2個(gè)版本號(hào)比大小鲁豪,比較簡單潘悼,沒啥邏輯。 廢話不多說爬橡,直接上代碼治唤,希望可以幫到大家。?
//檢測新版本
-(void)IterationVersion {
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSString *URL = @"https://itunes.apple.com/lookup?id=1106225692";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data != nil) {
NSMutableDictionary *jsondata = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *infoArray = [jsondata objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *lastVersion = [releaseInfo objectForKey:@"version"];
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];
self.trackViewUrl = trackViewUrl;
int? lastVersionnum = [[lastVersion stringByReplacingOccurrencesOfString:@"." withString:@""]intValue];
int? currentVersionnum = [[currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""] intValue];
if (lastVersionnum > currentVersionnum) {
dispatch_sync(dispatch_get_main_queue(), ^(){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新糙申,是否前往更新宾添?" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:@"取消", nil];
alert.tag = 10000;
[alert show];
});
}
}
}
}];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==10000) {
if (buttonIndex==0) {
NSURL *url = [NSURL URLWithString:self.trackViewUrl];
[[UIApplication sharedApplication]openURL:url];
}
}
}
使用起來也比較簡單,直接在AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}代理方法里 調(diào)用即可[self IterationVersion];