博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS简单实现版本更新检测
阅读量:5910 次
发布时间:2019-06-19

本文共 7933 字,大约阅读时间需要 26 分钟。

////  HYBVersionManager.h////  Created by 黄仪标 on 15/1/27.//#import 
// 应用已经发布到APP Store后才会在Itunes上有应用的链接// 所以版本检测必须是已经发布过才能做// 在真正实现功能时,需要替换成真正的链接// @requared#define kAppStoreLink @""#define kItunsLink @""/*! * 版本管理器 */@interface HYBVersionManager : NSObject/*! * @brief 单例方法 */+ (HYBVersionManager *)sharedVersionManager;/* * 调用此方法来执行版本检测 * @param type */- (void)checkVersion:(int)type;@end

////  HYBVersionManager.m////  Created by 黄仪标 on 15/1/27.//#import "HYBVersionManager.h"#import "UIAlertView+Blocks.h"#define kRequestTimeOut 60.0@interface HYBVersionManager () {  int         _type;  UIAlertView *_alertView;}@end@implementation HYBVersionManager/*! * @brief 单例方法 */+ (HYBVersionManager *)sharedVersionManager {  static HYBVersionManager *sharedObject = nil;  static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{    if (!sharedObject) {      sharedObject = [[self alloc] init];    }  });    return sharedObject;}- (instancetype)init {  if (self = [super init]) {    _type = 0; // 自动检测  }  return self;}- (void)checkVersion:(int)type {  _type = type;    [self checkAppStoreVersion];}- (void)checkAppStoreVersion {  if ([NSThread isMainThread]) {    [self performSelectorInBackground:@selector(checkAppStoreVersion) withObject:nil];    return;  }    @autoreleasepool  {    //prevent concurrent checks    static BOOL checking = NO;        if (checking) return;    checking = YES;        NSError *error = nil;    NSURLResponse *response = nil;    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kItunsLink]                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad                                         timeoutInterval:kRequestTimeOut];    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];    NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;    if (data && statusCode == 200) {      error = nil;      id json = nil;      if ([NSJSONSerialization class]) {        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];        json = [dict[@"results"] lastObject];      } else {        json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];      }            if (!error) {        // 获取到appstore上最新的版本号        NSString *latestVersion = [self valueForKey:@"version" inJSON:json];        NSString *localVersion = [self appLocalVersion];        [self check:latestVersion localVersion:localVersion];      }    }        // finished    checking = NO;  }}- (void)check:(NSString *)latestVersion localVersion:(NSString *)localVersion {  if ([latestVersion compare:localVersion] == NSOrderedDescending) { // 有新版本    [self showPromptForUpdate];  } else if ([latestVersion compare:localVersion options:NSNumericSearch] == NSOrderedSame) {// 已经是最新版本    if (_type == 1) { // 手动      [self showMessage];    }  }}- (void)showMessage {  if (_alertView) {    _alertView.hidden = YES;    [_alertView removeFromSuperview];    _alertView = nil;  }    UIAlertView *alert = [UIAlertView showWithTitle:@""                                          message:@"当前版本已经是最新版本!"                                         okButton:@"确定"                                     cancelButton:nil];  _alertView = alert;}- (void)showPromptForUpdate {  [UIAlertView showWithTitle:nil                     message:@"有新版本发布了,亲,快去更新吧!"           cancelButtonTitle:@"暂不更新"           otherButtonTitles:@[@"立即更新"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {                                 if (buttonIndex == 0) {                                                                    } else {                                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kAppStoreLink]];                                 }                               }];}/*! * @brief 获取app本地的版本号 */- (NSString *)appLocalVersion {  NSDictionary *info = [[NSBundle mainBundle] infoDictionary];  NSString *version = [info objectForKey:@"CFBundleVersion"];    return [version stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];}- (NSString *)valueForKey:(NSString *)key inJSON:(id)json {  if ([json isKindOfClass:[NSString class]]) {    //use legacy parser    NSRange keyRange = [json rangeOfString:[NSString stringWithFormat:@"\"%@\"", key]];    if (keyRange.location != NSNotFound) {      NSInteger start = keyRange.location + keyRange.length;      NSRange valueStart = [json rangeOfString:@":" options:(NSStringCompareOptions)0 range:NSMakeRange(start, [(NSString *)json length] - start)];      if (valueStart.location != NSNotFound) {        start = valueStart.location + 1;        NSRange valueEnd = [json rangeOfString:@"," options:(NSStringCompareOptions)0 range:NSMakeRange(start, [(NSString *)json length] - start)];        if (valueEnd.location != NSNotFound) {          NSString *value = [json substringWithRange:NSMakeRange(start, valueEnd.location - start)];          value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];          while ([value hasPrefix:@"\""] && ![value hasSuffix:@"\""]) {            if (valueEnd.location == NSNotFound) {              break;            }            NSInteger newStart = valueEnd.location + 1;            valueEnd = [json rangeOfString:@"," options:(NSStringCompareOptions)0 range:NSMakeRange(newStart, [(NSString *)json length] - newStart)];            value = [json substringWithRange:NSMakeRange(start, valueEnd.location - start)];            value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];          }                    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];          value = [value stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];          value = [value stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];          value = [value stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];          value = [value stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];          value = [value stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];          value = [value stringByReplacingOccurrencesOfString:@"\\t" withString:@"\t"];          value = [value stringByReplacingOccurrencesOfString:@"\\f" withString:@"\f"];          value = [value stringByReplacingOccurrencesOfString:@"\\b" withString:@"\f"];                    while (YES) {            NSRange unicode = [value rangeOfString:@"\\u"];            if (unicode.location == NSNotFound || unicode.location + unicode.length == 0) {              break;            }                        uint32_t c = 0;            NSString *hex = [value substringWithRange:NSMakeRange(unicode.location + 2, 4)];            if (hex != nil) {              NSScanner *scanner = [NSScanner scannerWithString:hex];              [scanner scanHexInt:&c];            }                        if (c <= 0xffff) {              value = [value stringByReplacingCharactersInRange:NSMakeRange(unicode.location, 6) withString:[NSString stringWithFormat:@"%C", (unichar)c]];            } else {              //convert character to surrogate pair              uint16_t x = (uint16_t)c;              uint16_t u = (c >> 16) & ((1 << 5) - 1);              uint16_t w = (uint16_t)u - 1;              unichar high = 0xd800 | (w << 6) | x >> 10;              unichar low = (uint16_t)(0xdc00 | (x & ((1 << 10) - 1)));                            value = [value stringByReplacingCharactersInRange:NSMakeRange(unicode.location, 6) withString:[NSString stringWithFormat:@"%C%C", high, low]];            }          }          return value;        }      }    }  } else {    return json[key];  }  return nil;}@end
之前使用了iLink库,可是后来出现了很多的问题,于是不得不去掉,自己写一个,完全自主控制了。

这里功能很简单,一种是自动检测,也就是在应用启动的时候调用,

另一种是手动检测,一般是放在设置中的检查版本更新时,手动检测。

转载地址:http://wmtpx.baihongyu.com/

你可能感兴趣的文章
顶部BANNER
查看>>
使用IDEA部署项目到远程服务器
查看>>
Python3求英文文档中每个单词出现的次数并排序
查看>>
【VS】使用vs2017自带的诊断工具(Diagnostic Tools)诊断程序的内存问题
查看>>
Excel导出文件流下载
查看>>
Ubuntu 12.04将默认集成Landscape管理套件【转】
查看>>
Windows 7安装超级终端连接COM口设备
查看>>
迭代器模式
查看>>
oracle获取过去两年的今天时间
查看>>
微信小程序登录JAVA后台
查看>>
如何恢复回收站删除的文件
查看>>
Android 模拟器下载、编译及调试
查看>>
findbugs静态代码分析工具使用教程
查看>>
TERMIOS详解【转】
查看>>
BZOJ2216: [Poi2011]Lightning Conductor(DP 决策单调性)
查看>>
js 终止 forEach 循环
查看>>
js数组实现不重复插入数据
查看>>
HDU 4089 Activation
查看>>
Linux 上安装 Subversion
查看>>
PHP5.4第二天——数组、多维数组和数组函数
查看>>