今天公司要给APP加上“检查更新”这个服务,因为在上一家公司做过,所以没花太多时间就完成了。不过还是看以前公司的代码完成的,所以觉得还是写下来比较好,以免以后自己给忘了。
因为比较简单,所以下面直接上代码:
.h里面的代码
#import <Foundation/Foundation.h>
@interface VersionUpdateObject : NSObject
+(instancetype)sharedInstance;
- (void)updateApp;
@end
.m里面的代码
#import "VersionUpdateObject.h"
#import "SVProgressHUD.h"
#define kAPP_URL @"http://itunes.apple.com/lookup?id="
#define kAppID @"你自己的APPID"
@interface VersionUpdateObject ()
{
NSTimer *timer;
}
@property(nonatomic,strong)NSString *trackViewUrl;//更新时用到的地址
@end
@implementation VersionUpdateObject
+(instancetype)sharedInstance
{
static id _versionUpdate;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_versionUpdate = [[self alloc] init];
});
return _versionUpdate;
}
#pragma mark - 检查更新
- (void)updateApp
{
[SVProgressHUD show];
if (!timer) {
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(SvProgressDismiss) userInfo:nil repeats:NO];
}
NSString *urlStr = [NSString stringWithFormat:@"%@%@", kAPP_URL, kAppID];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//网络请求
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSError *err;
if (data.length == 0) {
return;
}
NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
[self SvProgressDismiss];
if (err) {
NSLog(@"%@", err.description);
return;
}
NSArray *resultArray = [appInfoDict objectForKey:@"results"];
if (![resultArray count]) {
return;
}
NSDictionary *infoDict = [resultArray objectAtIndex:0];
//获取服务器上应用的最新版本号
NSString *updateVersion = infoDict[@"version"];
NSString *newVersionStr = [updateVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
//_trackViewUrl : 更新的时候用到的地址
_trackViewUrl = infoDict[@"trackViewUrl"];
//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSString *oldVersionStr = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
// oldVersionStr = @"1.2";
//判断两个版本是否相同
if ([oldVersionStr doubleValue] < [newVersionStr doubleValue]) {
NSString *messageStr = @"发现新版本";
dispatch_sync(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:messageStr delegate:self cancelButtonTitle:@"暂不更新" otherButtonTitles:@"立即更新", nil];
alert.tag = [kAppID intValue];
[alert show];
});
}else { //版本号和app store上的一致
if (!self.isAutoShowStatus) {//非自动更新
dispatch_sync(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
alert.tag = [kAppID intValue] + 1;
[alert show];
});
}
}
}];
[task resume];
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == [kAppID intValue])
{
if (buttonIndex == 1)
{ //点击”升级“按钮,就从打开app store上应用的详情页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
}
}
}
/**
超过20秒。则取消旋转
*/
-(void)SvProgressDismiss
{
NSLog(@"取消菊花旋转");
[SVProgressHUD dismiss];
[timer invalidate];
timer = nil;
}
网友评论