-(void)checkVersion
{
//每天進(jìn)行一次版本判斷
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
//獲取年-月-日
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSString *currentDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentDate"];
if ([currentDate isEqualToString:dateString]) {
return;
}
[[NSUserDefaults standardUserDefaults] setObject:dateString forKey:@"currentDate"];
NSString *newVersion;
NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=1139094792"];//這個(gè)URL地址是該app在iTunes connect里面的相關(guān)配置信息抚恒。其中id是該app在app store唯一的ID編號(hào)郁轻。
NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"通過appStore獲取的數(shù)據(jù)信息:%@",jsonResponseString);
NSData *data = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding];
//? ? 解析json數(shù)據(jù)
id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *array = json[@"results"];
for (NSDictionary *dic in array) {
newVersion = [dic valueForKey:@"version"];
}
NSLog(@"通過appStore獲取的版本號(hào)是:%@",newVersion);
//獲取本地軟件的版本號(hào)
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
NSString *msg = [NSString stringWithFormat:@"您的App不是最新版本媳板,請問是否更新"];
//對比發(fā)現(xiàn)的新版本和本地的版本
if ([newVersion floatValue] > [localVersion floatValue])
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示"message:msg preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
[alert addAction:[UIAlertAction actionWithTitle:@"現(xiàn)在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/yi-ka-tongbic-ban/id1139094792?l=en&mt=8"]];
//這里寫的URL地址是該app在app store里面的下載鏈接地址纤垂,其中ID是該app在app store對應(yīng)的唯一的ID編號(hào)。
NSLog(@"點(diǎn)擊現(xiàn)在升級(jí)按鈕");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"下次再說" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點(diǎn)擊下次再說按鈕");
}]];
}
}