博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发之检查更新
阅读量:6987 次
发布时间:2019-06-27

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

iOS设备检查更新版本:

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
#pragma mark - 检查更新
- (
void
)checkUpdateWithAPPID:(
NSString 
*)APPID
{
    
//获取当前应用版本号
    
NSDictionary 
*appInfo = [[
NSBundle 
mainBundle] infoDictionary];   
    
NSString 
*currentVersion = [appInfo objectForKey:@
"CFBundleVersion"
];
     
    
NSString 
*updateUrlString = [
NSString 
stringWithFormat:@
"http://itunes.apple.com/lookup?id=%@"
,APPID];
    
NSURL 
*updateUrl = [
NSURL 
URLWithString:updateUrlString];
    
versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];
    
[versionRequest setRequestMethod:@
"GET"
];
    
[versionRequest setTimeOutSeconds:60];
    
[versionRequest addRequestHeader:@
"Content-Type" 
value:@
"application/json"
];
     
    
//loading view
    
CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@
"正在检查更新..."
];
    
checkingAlertView.userInteractionEnabled = 
YES
;
    
[
self
.navigationController.view addSubview:checkingAlertView];
    
[checkingAlertView release];
     
    
[versionRequest setCompletionBlock:^{
         
        
[checkingAlertView removeFromSuperview];
         
        
NSError 
*error = 
nil
;
        
NSDictionary 
*dict = [
NSJSONSerialization 
JSONObjectWithData:[versionRequest responseData] options:
NSJSONReadingMutableContainers 
error:&error];
        
if 
(!error) {
            
if 
(dict != 
nil
) {
                
//            DLog(@"dict %@",dict);
                
int 
resultCount = [[dict objectForKey:@
"resultCount"
] integerValue];
                
if 
(resultCount == 1) {
                    
NSArray 
*resultArray = [dict objectForKey:@
"results"
];
                    
//                DLog(@"version %@",[resultArray objectAtIndex:0]);
                    
NSDictionary 
*resultDict = [resultArray objectAtIndex:0];
                    
//                DLog(@"version is %@",[resultDict objectForKey:@"version"]);
                    
NSString 
*newVersion = [resultDict objectForKey:@
"version"
];
                     
                    
if 
([newVersion doubleValue] > [currentVersion doubleValue]) {
                        
NSString 
*msg = [
NSString 
stringWithFormat:@
"最新版本为%@,是否更新?"
,newVersion];
                        
newVersionURlString = [[resultDict objectForKey:@
"trackViewUrl"
copy
];
                        
DLog(@
"newVersionUrl is %@"
,newVersionURlString);
                        
//                    if ([newVersionURlString hasPrefix:@"https"]) {
                        
//                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];
                        
//                    }
                        
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@
"提示" 
message:msg delegate:
self 
cancelButtonTitle:@
"暂不" 
otherButtonTitles:@
"立即更新"
nil
];
                        
alertView.tag = 1000;
                        
[alertView show];
                        
[alertView release];
                    
}
else
                    
{
                        
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@
"提示" 
message:@
"您使用的是最新版本!" 
delegate:
self 
cancelButtonTitle:
nil 
otherButtonTitles:@
"确定"
nil
];
                        
alertView.tag = 1001;
                        
[alertView show];
                        
[alertView release];
                    
}
                
}
            
}
        
}
else
        
{
            
DLog(
"error is %@"
,[error debugDescription]);
        
}
    
}];
     
    
[versionRequest setFailedBlock:^{
        
[checkingAlertView removeFromSuperview];
         
        
CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@
"操作失败,请稍候再试!"
];
        
[
self
.navigationController.view addSubview:alertView];
        
[alertView release];
        
[alertView selfRemoveFromSuperviewAfterSeconds:1.0];
    
}];
     
    
[versionRequest startSynchronous]; 
}
 
- (
void
)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(
NSInteger
)buttonIndex
{
    
DLog(@
"newVersionUrl  is %@"
,newVersionURlString);
    
if 
(buttonIndex) {
        
if 
(alertView.tag == 1000) {
            
if
(newVersionURlString)
            
{
                
[[UIApplication sharedApplication] openURL:[
NSURL 
URLWithString:newVersionURlString]];
            
}
        
}
    
}
}

 

来源:

本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3539691.html,如需转载请自行联系原作者

你可能感兴趣的文章
Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
查看>>
安装paramiko(python ssh)模块
查看>>
FreeBSD10 gnome2 桌面安装手册 (通过镜像iso)
查看>>
Kafka文件存储机制那些事
查看>>
Intent 的回跳
查看>>
C++项目中的extern "C" {}
查看>>
【C++】指向函数的指针,指向重载函数的指针,指向类成员的指针
查看>>
我的友情链接
查看>>
pfSense LAGG(链路聚合)设置
查看>>
关于springmvc的restful接口
查看>>
Linux磁盘和文件系统管理(一)
查看>>
梦里花落知多少?
查看>>
Maven中多模块的编译顺序
查看>>
职场中没有“朋友”
查看>>
React 概要
查看>>
Centos 7 系统安装完毕修改网卡名为eth0
查看>>
centos7配置mysql的主从复制
查看>>
Oracle数据库日常维护
查看>>
安卓显示添加图片后运行报错
查看>>
如何解决linux下apache启动时httpd: apr_sockaddr_info_get() failed for 报错
查看>>