iOS版本检测代码(更新版本)

来源:互联网 发布:ksweb mysql 编辑:程序博客网 时间:2024/05/16 19:07
想要检测App的最新版本,首先你得知道当前App在AppStore中的版本是什么,在根据你的程序的版本也就是这个的第二个版本号,因为第二个是你的发布版本,上传App的时候主要检测的是这个,它们俩进行对比之后,进行判断,如果结果一样就发个弹窗说是最新版本,如果不是就连接到你的App的下载地址来更新应用。这个应用的下载地址可以再iTunes中获取如下图

这样就获取的你的App的地址了。
下面发代码,这个代码用到GCD多线程,因为直接写的话是主线程会发生卡顿的,因为有网络请求

dispatch_queue_t mainQueue =dispatch_get_main_queue();

           dispatch_queue_t globalQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

           NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];

           NSString *currentVersion = [infoDicobjectForKey:@"CFBundleVersion"];

           dispatch_async(globalQueue, ^{

                

                NSString *URL =@"http://itunes.apple.com/lookup?id=你自己的AppID";

                NSMutableURLRequest *request = [[NSMutableURLRequestalloc]init];

                [requestsetURL:[NSURLURLWithString:URL]];

                [requestsetHTTPMethod:@"POST"];

               NSHTTPURLResponse *urlResponse =nil;

               NSError *error =nil;

               NSData *recervedData = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&urlResponse error:&error];

                

                

                NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:recervedDataoptions:NSJSONReadingMutableContainerserror:&error];

               NSArray *infoArray = [dicobjectForKey:@"results"];

               NSLog(@"%@", infoArray);

               if ([infoArraycount]) {

                   NSDictionary *releaseInfo = [infoArrayobjectAtIndex:0];

                   NSString *lastVersion = [releaseInfoobjectForKey:@"version"];

                    

                   if (![lastVersionisEqualToString:currentVersion]) {

                       dispatch_async(mainQueue, ^{

                            

                            UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"更新"message:@"有新的版本更新,是否前往更新?"delegate:selfcancelButtonTitle:@"关闭"otherButtonTitles:@"更新",nil];

                            alert.tag =10000;

                            [alertshow];

                        });

                    }

                   else

                    {

                       dispatch_async(mainQueue, ^{

                            

                            UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"更新"message:@"此版本为最新版本"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

                            alert.tag =10001;

                            [alertshow];

                        });

                    }

                }

            });

            

        }



这个代码是连接更新的地址

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

   if (alertView.tag==10000) {

       if (buttonIndex==1) {

            NSURL *url = [NSURLURLWithString:@"https://itunes.apple.com/cn/app/ji-xun-xin-wen/id962735629?mt=8"];(这里填写的是你自己的App的连接)

            [[UIApplicationsharedApplication]openURL:url];

        }

    }

}


好了,分享完毕,谢谢大家!
小弟初学OC,不对的地方请大家指出,在此感谢!



0 0
原创粉丝点击