简谈思路:
当app出现在应用前台的时候,我们会在应用内检测当前手机上安装的app版本edition1,并且同时获取app store中该app当前版本edition2(xx.xx.xx),对这二个版本做比较(去掉“.”edition换算成整形加减法),如果edition2大于edition1,说明有版本更新,在app store下载自动升级。
代码实现:
//
// APPUpdateManager.m
// MoveBand
//
#import "APPUpdateManager.h"
#import
#import "ChangeLanguage.h"
@interface APPUpdateManager ()<NSURLConnectionDataDelegate,UIAlertViewDelegate>
//{
// NSString *version; //版本信息
// NSString *trackViewUrl; //app下载地址
//}
@property (nonatomic,copy) NSString *version;//版本信息
@property (nonatomic,copy) NSString *trackViewUrl;//app下载地址
@end
@implementation APPUpdateManager
+ (instancetype)shareInstance{
static APPUpdateManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[APPUpdateManager alloc]init];
});
return manager;
}
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
return self;
}
- (void)appEnterForeground{
NSLog(@"will appEnterForeground");
NSInteger latestCheckTime = [[NSUserDefaults standardUserDefaults] integerForKey:@"latestCheckTime"];
NSInteger currentTimeInter = [[NSDate date]timeIntervalSince1970];
if (currentTimeInter - latestCheckTime > 3600 * 24) {
[[NSUserDefaults standardUserDefaults]setInteger:currentTimeInter forKey:@"latestCheckTime"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSString *urlStr = @"https://itunes.apple.com/lookup?id=1336869336";//1336869336应用的ID
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:req delegate:self];
}
// 1492588093
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSError *error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary *appInfo = (NSDictionary *)jsonObject;
NSArray *infoContent = [appInfo objectForKey:@"results"];
if (infoContent.count == 0) {
return;
}
self.version = [[infoContent objectAtIndex:0]objectForKey:@"version"];
NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
//#warning joe 版本号记得是a.a.aa
NSInteger localVersion = [[currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
NSInteger latestVersion = [[self.version stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
if (localVersion < latestVersion ) {
self.trackViewUrl = [[infoContent objectAtIndex:0]objectForKey:@"trackViewUrl"];
[self alertViewShow];
}
}
- (void)alertViewShow{
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:[[ChangeLanguage bundle] localizedStringForKey:@"new_edition" value:@"" table:@"English"] message:self.version delegate:self cancelButtonTitle:[[ChangeLanguage bundle] localizedStringForKey:@"Cancel" value:@"" table:@"English"] otherButtonTitles: [[ChangeLanguage bundle] localizedStringForKey:@"Sure" value:@"" table:@"English"], nil];
[alertview show];//NSLocalizedString(@"IDS_FIND_NEW_DEVICE",@"发现新版本")NSLocalizedString(@"IDS_ENSURE",@"确定")NSLocalizedString(@"IDS_CANCEL",@"取消")
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:self.trackViewUrl]];
}
}
@end