iOS 版本更新提示、新特性启动页面思路 —— HERO博客

来源:互联网 发布:双色球算法必中6红246 编辑:程序博客网 时间:2024/06/02 16:43

简单整理iOS开发中,面对版本更新提示、新特性启动页面时的思考思路。

首先认清两者区别,版本更新的提示可能只要版本变动就会提示需要重复提醒。而新特性页面可能只出现在比较大的版本更新中,通常只显示一次。所以需要分开进行判断,根据各自的需求设计代码。

还有需要注意的一点,iOS系统不同于安卓,home键退出程序后,除非手动关闭程序,不然程序会一直保留在后台,启动在后台的app和已关闭的app会进入AppDelegate不同的方法中,需要在对应的方法中分别设计代码。


简单整理了思考思路,如图12-1。

分析逻辑之前简述一下上图的判断中共用到了3个不同的版本号,分别是:

App Store 中应用的版本号:提交版本苹果审核之后,苹果商城可以看到提供下载使用的版本号。

当前软件的版本号:Info中的Bundle version,开发者提交新版本时手动填写,用户下载新版本后自动更新为苹果商城的版本号。

本地持久化的版本号:手动存储在沙盒中,记录最新版本。

在新打开app的时候会进入到AppDelegate的didFinishLaunchingWithOptions的方法中,首先判断苹果商城和当前软件的版本号,如果苹果商城的版本号更高,说明有新版本,显示新版本更新提示,由用户选择是否更新。反之,苹果商城没有新版本,再判断当前软件和本地的版本号,如果当前软件的版本号更高,显示版本新特性页面,然后保存新的版本号到本地。

由后台打开进入app时会调用AppDelegate的applicationWillEnterForeground的方法中,判断苹果商城和当前软件的版本号,查看是否有版本更新。

下面贴一下相关代码,部分代码并不完善,仅供学习参考:

@implementation AppDelegatestatic NSString * const key = @"CFBundleVersion";- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        //appStore新版本检测    NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1037684"];    [self Postpath:url];        //获得当前软件的版本号    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];        //取出沙盒中存储的上次使用软件的版本号    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    NSString *lastVersion = [defaults stringForKey:key];        //以1.3.0格式为列,判断前3个字符,小更新最后1位改变没有新特性显示    NSString *str1 = [currentVersion substringToIndex:3];    NSString *str2 = [lastVersion substringToIndex:3];        if ([str1 isEqualToString:str2]) {        self.window.rootViewController = [[AViewController alloc] init];    }else {        //新版本        self.window.rootViewController = [[BViewController alloc] init];        //存储新版本        [defaults setObject:currentVersion forKey:key];        [defaults synchronize];    }        return YES;}//进入前台- (void)applicationWillEnterForeground:(UIApplication *)application {        // appStore新版本检测    NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"1037684"];    [self Postpath:url];}-(void)Postpath:(NSString *)path{    NSURL *url = [NSURL URLWithString:path];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData                                                       timeoutInterval:10];    [request setHTTPMethod:@"POST"];        NSOperationQueue *queue = [NSOperationQueue new];        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) {        NSMutableDictionary *receiveStatusDic = [[NSMutableDictionary alloc] init];                if (data) {            NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];                        if ([[receiveDic valueForKey:@"resultCount"] intValue] > 0) {                [receiveStatusDic setValue:@"1" forKey:@"status"];                [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];                            }else {                [receiveStatusDic setValue:@"-1" forKey:@"status"];            }                    }else {            [receiveStatusDic setValue:@"-1" forKey:@"status"];        }                [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];    }];}- (void)receiveData:(NSMutableDictionary *)receiveStatusDic{    //appStore版本号    NSString *appStoreVersion = [receiveStatusDic objectForKey:@"version"];    //获得当前软件的版本号    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];    NSString *str1 = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];    NSString *str2 = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];        //新版本    if ([str2 intValue] < [str1 intValue]) {       //show alertView ...    }}@end


3 0
原创粉丝点击