zoukankan      html  css  js  c++  java
  • iOS 检查版本号的代码

    - (void)checkNewVersion{

        if ([@"appStore" isEqualToString:CHANNEL]) {

            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

            NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", APPLE_ID];

            [manager POST:url sign:nil token:self.user.token parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

                id obj = [responseObject objectForKey:@"results"];

                if (obj && [obj isKindOfClass:[NSArray class]]) {

                    versionResult = [obj lastObject];    //NSMutableDictionary     *versionResult;

                    if ([versionResult isKindOfClass:[NSMutableDictionary class]]) {//versionResult是全局的

                        NSString *newVersion = [versionResult objectForKey:@"version"];//appstore的版本

                        NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

                        NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];//本地软件版本

                        if (newVersion && [currentVersion compare:newVersion] == NSOrderedAscending) {

                            UIAlertView *newVersionAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"检测到可用的新版本%@",newVersion]

                                                                                      message:[NSString stringWithFormat:@"%@",[versionResult objectForKey:@"releaseNotes"]]

                                                                                     delegate:self

                                                                            cancelButtonTitle:@"稍后再说"

                                                                            otherButtonTitles:@"马上升级",nil];

                            newVersionAlert.tag = NEW_VERSION_ALERT;//用于标记当前alert

                            [newVersionAlert show];

                        }

                    }

                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                NSLog(@"Error: %@", error);

            }];

        } else {

            AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

            NSString *url = CHECK_VERSION_URL;

            [manager POST:url sign:nil token:self.user.token parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

                NSString *newVersion = [responseObject objectForKey:@"vcode"];

                NSString *newDesc = [responseObject objectForKey:@"vdesc"];

                NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

                NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];//本地软件版本

                if (newVersion && [currentVersion compare:newVersion] == NSOrderedAscending) {

                    UIAlertView *newVersionAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"检测到可用的新版本%@",newVersion]

                                                                              message:newDesc

                                                                             delegate:self

                                                                    cancelButtonTitle:@"稍后再说"

                                                                    otherButtonTitles:@"马上升级",nil];

                    newVersionAlert.tag = NEW_VERSION_ALERT;//用于标记当前alert

                    [newVersionAlert show];

                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                NSLog(@"Error: %@", error);

            }];

        }

    }

    //点击马上升级的代理

    #pragma mark UIAlertView Delegate

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

        [super alertView:alertView clickedButtonAtIndex:buttonIndex];

        if (alertView.tag == NEW_VERSION_ALERT) {

            //新版本提示框

            if (buttonIndex == 1) {

                if ([@"appStore" isEqualToString:CHANNEL]) {

                    //跳转到App Store

                    if ([versionResult isKindOfClass:[NSMutableDictionary class]]) {

                        UIApplication *application = [UIApplication sharedApplication];

                        NSString *trackViewUrl = [versionResult objectForKey:@"trackViewUrl"];

                        [application openURL:[NSURL URLWithString:[trackViewUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

                    }

                } else {

                    UIApplication *application = [UIApplication sharedApplication];

                    NSString *trackViewUrl = APP_DOWNLOAD_URL;

                    [application openURL:[NSURL URLWithString:[trackViewUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

                }

            }

        }

    }

     

     

    //黄色标记的为,#define得到的

    如果需要检查版本的话,可直接复制一下代码,其中再大致修改一下

  • 相关阅读:
    sklearn
    Scrapy
    正则表达式re
    BeautifulSoup
    requests
    Python网络爬虫与信息提取
    Matplotlib
    Pandas
    NumPy
    制约大数据处理能力的几个问题
  • 原文地址:https://www.cnblogs.com/lyz0925/p/4995049.html
Copyright © 2011-2022 走看看