iOS地图iOS 版本更新征服iOS

ios 检查更新机制

2017-01-21  本文已影响227人  5b6a9d22d4f0

最近使用网上的ios检查更新出现了问题,发现大部分的更新机制无非是获取服务器版本号或者获取商店版本号进行对比来进行,但是就是这样的一个问题,我们崩了。。。。
出现问题在于app版本升级之后到服务器升级之前的这一段时间,是问题发生的可能点(ps:app升级前服务器升级了怎么办,这不是没事找事么-。-除非确定升级服务器跟app旧版本没有冲突的前提下)
那么我们怎么来写一个比较完善一些些的检查更新呢?
首先我们应该先获取到服务器的版本号和app版本号,比较后,在进行app版本号和商店版本号比较,然后再进行判断,我搞了一个category(有点好使这个东西!!!)

#import <Foundation/Foundation.h>
@interface NSObject (Update)
/*
    更新app
    ServerVerSion : 服务器返回的版本号
    BundleVersion : 工程项目本身的版本号(app旧版本号)
 */
+ (BOOL)updateAppWithServerVerSion:(NSString *)ServerVerSion;
@end

实现它:

#import "NSObject+Update.h"
@implementation NSObject (Update)

/*
    更新app
    ServerVerSion : 服务器返回的版本号
    BundleVersion : 工程项目本身的版本号(app旧版本号)
    iTunesAppVersionWithAppID : 获取app store 版本号
 */
+ (BOOL)updateAppWithServerVerSion:(NSString *)ServerVerSion
{
    NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
    NSString *BundleVersion = [dict objectForKey:@"CFBundleShortVersionString"];
    if (![ServerVerSion isEqualToString:BundleVersion] && ![BundleVersion isEqualToString:[NSString iTunesAppVersionWithAppID]]) {
        return 1;
    }
    return  0;
}
@end

上面有一个iTunesAppVersionWithAppID : 获取app store 版本号,稍微区分一下category不同的功能,我又搞了一个-。-(这玩意儿上瘾)

#import <Foundation/Foundation.h>
@interface NSString (URLs)
/*
    URL iTunes app网址
 */
+ (NSString *)iTunesAppURLWithAppID;
/*
    获取iTunes app 版本号
 */
+ (NSString *)iTunesAppVersionWithAppID;
/*
    获取iTunesApp txt文件内容
 */
+ (NSArray *)iTunesAppTextWithAppID;
@end

实现它:

#import "NSString+URLs.h"
@implementation NSString (URLs)
/*
    URL iTunes app网址
 */
+ (NSString *)iTunesAppURLWithAppID
{
    NSArray *resultArray = [self iTunesAppTextWithAppID];
    NSDictionary *infoDict = resultArray.lastObject;
    NSString *trackViewUrl = [infoDict objectForKey:@"trackViewUrl"];
    return trackViewUrl;
}
/*
 获取iTunes app 版本号
 */
+ (NSString *)iTunesAppVersionWithAppID
{
    NSArray *resultArray = [self iTunesAppTextWithAppID];
    NSDictionary *infoDict = resultArray.lastObject;
    NSString *version = [infoDict objectForKey:@"version"];
    return version;
}
/*
 获取iTunesApp txt文件内容
 */
+ (NSArray *)iTunesAppTextWithAppID
{
    NSError *error;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@",APPLE_ID]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil];
    if (error) {
        return nil;
    }
    NSArray *resultArray = [appInfoDict objectForKey:@"results"];
    if (![resultArray count]) {
        return nil;
    }
    return resultArray;
}
@end

ok,在返回服务器版本号的时候调用第一个category方法就可以啦。(ps:提示框啥的我就不赘述了,好吧!)
小伙伴有啥好的意见或者优化建议尽管来说说吧。

上一篇 下一篇

猜你喜欢

热点阅读