iOS --------- 强制提示用户版本升级

来源:互联网 发布:形容程序员的武功境界 编辑:程序博客网 时间:2024/05/16 11:08

//

//  LoginAnRegisterViewController.m

//  HengTaiXinGolf

//

//  Created by ouyangrong on 15/7/15.

//  Copyright (c) 2015 HengTaiXinGolf. All rights reserved.

//


#import "LoginAnRegisterViewController.h"

#import "MyLoginViewController.h"

#import "MyRegisterViewController.h"



@interface LoginAnRegisterViewController ()<UIAlertViewDelegate>


@end


@implementation LoginAnRegisterViewController

{

    NSString *appVersion;

    

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    [selfversionButton];


    [selfcreatLoginAndRegisterView];

}


//城市高尔夫版本更新实现方法

-(void)versionButton{

    //获取发布版本的version

    NSString *string = [NSStringstringWithContentsOfURL:[NSURLURLWithString:ITUNESLOOKUPURL]encoding:NSUTF8StringEncodingerror:nil];

    

    if (string != nil && [string length] > 0 && [string rangeOfString:@"version"].length ==7) {

        //        NSLog(@"------版本号2--------------%@",string);

        

        [self checkAppUpdate:string];

        

    }

}


#pragma mark --------比较当前版本与新上线版本做比较

-(void)checkAppUpdate:(NSString *)appInfo{

    //获取当前版本

    NSString *version = [[NSBundlemainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    

    //构建版本

    //   NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

    

    NSString *appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@"\"version\":"].location +10];

    appInfo1 = [[appInfo1 substringToIndex:[appInfo1rangeOfString:@","].location]stringByReplacingOccurrencesOfString:@"\""withString:@""];

    //判断,如果当前版本与发布版本不相同,则进入更新;如果相等,那么当前就是最高版本

    //    NSLog(@"------------新版本:%@----------------当前版本:%@",appInfo1,version);

    appVersion = appInfo1;

    if (![appInfo1 isEqualToString:version]) {

        

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:[NSStringstringWithFormat:@"《城市高尔夫》有新的版本%@,点击到AppStore升级新版本!",appVersion]delegate:selfcancelButtonTitle:@"知道了"otherButtonTitles:nil,nil];

        alert.delegate = self;

        [alert addButtonWithTitle:@"前往更新"];

        [alert show];

        alert.tag = 20;

        //

        //        UILocalNotification *localNotification = [[UILocalNotification alloc]init];

        //        if (localNotification) {

        //            // 设置通知的提醒时间

        ////            NSDate *date = [NSDate dateWithTimeIntervalSinceNow:3];

        ////            localNotification.fireDate = date;

        //            // 设置通知的提醒时间

        //            NSDate *currentDate = [NSDate date];

        //            //设置时区

        //            localNotification.timeZone = [NSTimeZone defaultTimeZone]; //使用本地时区

        //            localNotification.fireDate = [currentDate dateByAddingTimeInterval:5.0];

        //            //设置重复间隔

        //            localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;

        //

        ////            localNotification.fireDate = [[[NSDate alloc]init]dateByAddingTimeInterval:3];

        ////            NSTimeZone*zone=[[NSTimeZone alloc]initWithName:@"GMT+8"];

        //            //设置提醒文字内容

        //            localNotification.alertBody = @"城市高尔夫有新的版本,点击到AppStore升级。";

        //            localNotification.alertAction = @"升级";

        //            //通知提示音使用默认的

        //            localNotification.soundName = UILocalNotificationDefaultSoundName;

        ////            [application scheduleLocalNotification:localNotification];

        ////            NSMutableDictionary *aUserInfo = [[NSMutableDictionary alloc]init];

        ////            aUserInfo[kLocalNotificationID] = @"LocalNotificationID";

        //            NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"LocalNotificationID" forKey:@"key"];

        //            localNotification.userInfo = infoDic;

        //

        //            // ios8后,需要添加这个注册,才能得到授权

        //

        //            if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {

        //                UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

        //                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type

        //                                                                                         categories:nil];

        //                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

        //            }

        //

        //            UIApplication *app = [UIApplication sharedApplication];

        //            [app scheduleLocalNotification:localNotification];

        //            

        //            

        //        }

        

    }else{

        //      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已是最高版本" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        //        [alert show];

        NSLog(@"已是最高版本!");

    }

    

}



#pragma mark---------UIAlertView 代理方法

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

    if (buttonIndex == 1 && alertView.tag == 20) {

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:ITUNESLOADURL]];

    }

    

}



//创建启动登录注册界面

-(void)creatLoginAndRegisterView{

    UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, IPHONE_WIDTH,IPHONE_HEIGHT)];

    imgView.image = [UIImageimageNamed:@"qdtp"];

    [self.viewaddSubview:imgView];

    //登录按钮

    UIButton *longinBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    longinBtn.frame = CGRectMake(20, IPHONE_HEIGHT-100, IPHONE_WIDTH/2-40,40);

    longinBtn.backgroundColor = kColor(1, 143,227);

    [longinBtn setTitle:@"登录"forState:UIControlStateNormal];

    longinBtn.clipsToBounds= YES;

    longinBtn.layer.cornerRadius=4;

    

    [longinBtn addTarget:selfaction:@selector(longinBtnClick)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:longinBtn];

    //注册按钮

    UIButton *registerBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

    


    

    registerBtn.frame =CGRectMake(IPHONE_WIDTH/2 +20, IPHONE_HEIGHT-100,IPHONE_WIDTH/2-40,40);

    registerBtn.backgroundColor = kColor(1, 143,227);

    registerBtn.clipsToBounds=YES;

    registerBtn.layer.cornerRadius=4;

    [registerBtn setTitle:@"注册"forState:UIControlStateNormal];

    [registerBtn addTarget:selfaction:@selector(registerBtnClick)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:registerBtn];

}

//进入登录界面

-(void)longinBtnClick{

    NSLog(@"进入登录界面");

//    ModalView* modalView2 = [[ModalView alloc] init];

//    UIViewController* presentingViewController = self.presentingViewController;

//    [self dismissViewControllerAnimated:YES completion:^{

//        [presentingViewController presentViewController:modalView2 animated:YES completion:nil];

    

    MyLoginViewController *myLVC = [[MyLoginViewControlleralloc]init];

//    [self dismissViewControllerAnimated:NO completion:nil];

//    myLVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myLVC];

    [selfpresentViewController:myLVC animated:NOcompletion:^{

       // [self.view removeFromSuperview];

    }];

}

//进入注册界面

-(void)registerBtnClick{

    NSLog(@"进入注册界面");

    MyRegisterViewController *myRVC = [[MyRegisterViewControlleralloc]init];

//    myRVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    [selfpresentViewController:myRVC animated:NOcompletion:^{

    }];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}





- (BOOL)shouldAutorotate

{

    return YES;

}

- (NSUInteger)supportedInterfaceOrientations

{

    returnUIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

    returnUIInterfaceOrientationPortrait;

}




/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


0 0
原创粉丝点击