iOS-App版本更新提示

//檢查版本更新json

-(void)VersionRequestApiapp

{異步

    //異步線程//處理耗時操做的代碼塊async

    dispatch_async(dispatch_get_global_queue(0, 0), ^{post

        //App內info.plist文件裏面版本號url

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

        CurrentVersion = infoDict[@"CFBundleShortVersionString"];//本地的版本號線程

        // 取得AppStore信息component

        MyURL = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1336413759"];orm

        [[ApiTool shareApiTool] post:MyURL params:nil success:^(NSDictionary *jsonDic) {

            NSArray *results = [jsonDic objectForKey:@"results"];

            AppStoreVersion = [results[0] objectForKey:@"version"];//appstore的版本號

            ReleaseNotes = [results[0] objectForKey:@"releaseNotes"];//更新描述

            trackViewUrl = [results[0] objectForKey:@"trackViewUrl"];//跳轉AppStore須要的url

            NSLog(@"AppStoreVersion:%@,ReleaseNotes:%@,trackViewUrl:%@",AppStoreVersion,ReleaseNotes,trackViewUrl);

            //比較版本號

            NSArray *curVerArr = [CurrentVersion componentsSeparatedByString:@"."];

            NSArray *appstoreVerArr = [AppStoreVersion componentsSeparatedByString:@"."];

            BOOL needUpdate = NO;//是否須要更新

            //比較版本號大小

            int maxv = (int)MAX (curVerArr.count, appstoreVerArr.count);

            int cver = 0;

            int aver = 0;

            for (int i = 0; i < maxv; i++)

            {

                if (appstoreVerArr.count > i)

                {

                    aver = [NSString stringWithFormat:@"%@", appstoreVerArr[i]].intValue;

                }

                else

                {

                    aver = 0;

                }

                if (curVerArr.count > i)

                {

                    cver = [NSString stringWithFormat:@"%@", curVerArr[i]].intValue;

                }

                else

                {

                    cver = 0;

                }

                

                if (aver == cver)

                {

                    continue;

                }

                else if (aver > cver)

                {

                    needUpdate = YES;

                    break;

                }

                else

                {

                    needUpdate = NO;

                    break;

                }

            }

            

            if (needUpdate) {

                //彈提示框

                NSLog(@"彈提示框");

                SVAlertView *alertView = [[SVAlertView alloc] init];

                [alertView

                 showAlertViewWithMessage:ReleaseNotes

                 andTitle:@"升級提示"

                 confirmTitle:@"立刻更新"

                 cancelTitle:@"之後再說"

                 ConfirmAction:^{

                     NSLog(@"跳到appstore");

                     //跳到appstore

                     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];

                 }

                 andCancelAction:^{

                     NSLog(@"點擊了取消");

                 }];

            }

        } failure:^(NSError *error) {

            [MyHUD showInView:ShareApp.window text:@"版本號獲取失敗"];

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

        }];

    });

}

相關文章
相關標籤/搜索