- 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
- //
// HKCheckAppVersionMgr.m
// HKTyy
//
// Created by isHakan on 17/3/24.
// Copyright © 2017年 liuhuakun. All rights reserved.
//
#import "HKCheckAppVersionMgr.h"
#import <UIKit/UIKit.h>
@interface HKCheckAppVersionMgr ()<UIAlertViewDelegate>
@property (nonatomic, strong) NSString *appId;
@end
@implementation HKCheckAppVersionMgr
+ (HKCheckAppVersionMgr *)sharedInstance
{
static HKCheckAppVersionMgr *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[HKCheckAppVersionMgr alloc] init];
});
return instance;
}
- (void)isUpdataApp:(NSString *)appId
{
NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]];
NSString *appMsg = [NSString stringWithContentsOfURL:appUrl encoding:NSUTF8StringEncoding error:nil];
NSDictionary *appMsgDict = [self jsonStringToDictionary:appMsg];
NSDictionary *appResultsDict = [appMsgDict[@"results"] lastObject];
NSString *appStoreVersion = appResultsDict[@"version"];
float newVersionFloat = [appStoreVersion floatValue];//新发布的版本号
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
float currentVersionFloat = [currentVersion floatValue];//使用中的版本号
//当前版本小于App Store上的版本&用户未点击不再提示
if (currentVersionFloat<newVersionFloat && ![self isAlertUpdataAgain])
{
self.appId = appId;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本,是否去更新?" delegate:self cancelButtonTitle:@"去更新" otherButtonTitles:@"下次再说",@"不再提示", nil];
[alertView show];
}
}
- (NSDictionary *)jsonStringToDictionary:(NSString *)jsonStr
{
if (jsonStr == nil)
{
return nil;
}
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
if (error)
{
//NSLog(@"json格式string解析失败:%@",error);
return nil;
}
return dict;
}
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",self.appId]]];
return;
}
if (buttonIndex==2)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IS_ALERT_AGAIN"];
[[NSUserDefaults standardUserDefaults] synchronize];
return;
}
}
- (BOOL)isAlertUpdataAgain
{
BOOL res = [[NSUserDefaults standardUserDefaults] objectForKey:@"IS_ALERT_AGAIN"];
return res;
}
@end
|
•
|
|