我们开发APP避免不了版本更新,现在我把我个人封装了一个小小的版本更新的东西跟大家分享一下。
首先我们创建一个UIWindow+Extension文件 我们在.H文件里写一个方法并定义几个宏。来设置一些数据信息。
- (void)chooseRootViewController;
#define kHarpyAlertViewTitle @"版本升级"
#define kHarpyCancelButtonTitle @"下次再说"
#define kHarpyUpdateButtonTitle @"马上升级"
#define kAppID @"XXXXXX" //这不用多说明什么
我们在看下.M文件的东西
- (void)chooseRootViewController{
//取出Window
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//加载info.plist,获取软件当前版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
//获取本地版本号
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *sandboxVersion = [defaults valueForKey:@"sandboxVersion"];
//判断是否显示新特性
if ([currentVersion compare:sandboxVersion] == NSOrderedDescending) {
// 显示新特性
IntroduceViewController *nfVc = [[IntroduceViewController alloc] init];
// 更新本地保存的版本号
[defaults setValue:currentVersion forKey:@"sandboxVersion"];
//可以做一些新特性东西
}else
{
// 设置根控制器
window.rootViewController = [ViewController sharedInstance];
//开启线程加载判断版本
[self performSelectorInBackground:@selector(test) withObject:nil];
}
}
-(void)test{
/**
* 版本更新
*/
NSString *storeString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kAppID];
NSURL *storeURL = [NSURL URLWithString:storeString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:storeURL];
[request setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( [data length] > 0 && !error ) { // Success
NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
// All versions that have been uploaded to the AppStore
NSArray *versionsInAppStore = [[appData valueForKey:@"results"] valueForKey:@"version"];
if ( ![versionsInAppStore count] ) { // No versions of app in AppStore
return;
}
else {
NSString *currentAppStoreVersion = [versionsInAppStore objectAtIndex:0];
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
if ([currentVersion compare:currentAppStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kHarpyAlertViewTitle
message:[NSString stringWithFormat:@"发现新版本(%@),是否更新", currentAppStoreVersion]
delegate:self
cancelButtonTitle:kHarpyCancelButtonTitle
otherButtonTitles:kHarpyUpdateButtonTitle, nil];
alertView.tag = [kAppID intValue];
[alertView show];
}
else {
MyLog(@"等于");
return;
}
}
});
}
}];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ( alertView.tag == [kAppID intValue] ) {
switch ( buttonIndex ) {
case 0:{
MyLog(@"----下次再说");
} break;
case 1:{ // Update
NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", kAppID];
NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
[[UIApplication sharedApplication] openURL:iTunesURL];
} break;
default:
break;
}
}
}
其实这个东西很简单的东西。。。。如果对你有帮助 请点赞