app 版本的更新

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ZachMan1993/article/details/52688616

static NSString *appid = @"1159247524"
static  NSString *appurl =@"http://itunes.apple.com/lookup?id=%@";

@interface AppDelegate ()<UIAlertViewDelegate>

@property(nonatomic,copy)NSString *UpdatesString;


@end


-(void)updatasApp

{

    NSString *appurlStr = [NSStringstringWithFormat:appurl,appid];

    NSURL *url = [NSURLURLWithString:appurlStr];

    

    NSURLRequest *requet = [[NSURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheDatatimeoutInterval:10];

    

    __weakAppDelegate *blockself = self;

    

    [NSURLConnectionsendAsynchronousRequest:requet queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData *_Nullable data, NSError *_Nullable connectionError)

     {

         if (connectionError ==nil)

         {

             //这里应该刷新UI

             //1.给数据源数组赋值

             //2,赋值结束之后.刷新UI([self.tableView reloadData])

             NSDictionary *appInfoDict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:nil];


             NSArray *resultArray = [appInfoDictobjectForKey:@"results"];

             

             if (resultArray.count )

             {

                 NSDictionary *infoDict = [resultArrayobjectAtIndex:0];

                 NSString *updateVersion = infoDict[@"version"];  //获取网络的版本号

                 

                 dispatch_async(dispatch_get_main_queue(), ^{

                     blockself.UpdatesString  = infoDict[@"trackViewUrl"];  //下载链接

                     [blockself getVersion:updateVersion];

                 });

             }

             }

    }];

}


-(void)getVersion:(NSString *)version

{

    NSDictionary *infodic = [[NSBundlemainBundle] infoDictionary];

    NSString *CurrentVersion = [infodicobjectForKey:@"CFBundleShortVersionString"];//获取当前版本

    if ([CurrentVersionisEqualToString:version])

    {

    }

    else{

        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"新版本更新"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"升级",nil];

        alertView.tag = 666;

        [alertView show];

    }

}


//判断用户点击了哪一个按钮

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

{

    if (alertView.tag == 666) {

        if (buttonIndex ==1) { //点击升级按钮,就从打开app store上应用的详情页面

            [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:self.UpdatesString]];

        }

    }

}



展开阅读全文

没有更多推荐了,返回首页