码迷,mamicode.com
首页 > 移动开发 > 详细

app的版本更新

时间:2015-07-23 12:06:08      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:app版本更新   登录时版本更新   运行版本更新   

版本更新大致分为两大类:app负责版本检测测与更新,通过苹果商店的自动更新机制进行自动更新。
通过苹果商店的自动更新机制进行自动更新是,应用更新时,手机会收到苹果商店的通知,并启动应用程序的自动更新下载应用并更新。但是可能出现手机收不到版本更新通知(由手机系统自己实现,并非弹出用户可见的push提示语)的情况和部分应用更新失败的情况,若你 Apple ID那么你也使用不了这类官方更新了。最麻烦的是用户一般都关闭了应用的自动更新,大都不希望在外面的移动蜂窝网络下自动更新大量应用,那样你流量会嗖嗖的莫名向上涨。
看来是自己的应用自己对自己负责更靠谱。版本更新一般分两类:强制更新和非强制性更新。当然强制更新是你不更新就不能用了,所以虽然霸道,但是也没有办法,通常是业务逻辑的大变更才会出现。非强制更新一般是不更新也能用,但是可能不更新部分功能你用不了或看不到,一般给你提供去苹果商店更新还是继续使用的选择。当然这种强制更新本质是有服务器来判断,需要拿当前版本号(服务器数据库中存储的应用版本号)和应用发送的版本号(存储在服务器与应用之间的相关会话中)相比较。当然服务器中的应用版本号在更新时要保证只能增大不能减少,若服务器的版本号变更需要向所有应用发送版本更新push通知。由于苹果审核应用的时间不能完全确定,并且苹果不希望有超过他掌控的版本更新,他做的又太完美,只能忽悠他了,等你收到你的应用上架的邮件时再更新你服务中数据库对应应用版本号了。
版本更新有三种实现:
第一种实现和场景,称为登录时版本更新,用户启动app登录或自动登录时,发送请求需要带本应用的当前版本号,服务返回的结果中包含版本更新信息,并且一般立刻弹出提示模态对话框让用户选择操作,当然强制更新只有去苹果商店一条路。模态对话框在点击后跳到苹果商店,由于跳转的过程中,应用被自动切换到后台,所以若是强制更新,从后台切换到前台时,由于以前的模态对话框由于点击消失还需要再弹出一个,非强制更新就不需要弹了该干啥就干啥去。
第二种实现和场景,称为运行时版本更新,当用户在使用时,应用有新版本上架(准确的说是服务器数据库中对应应用版本号更新),你的服务推送在线用户push消息(强制更新才发送,非强制更新不发这种强制更新push消息),当用户收到这个强制更新push消息,就把它记录到记录到全局变量中,当然不能弹出模态提示框,那样可能中断用户正在进行的操作,很不友好,当用户操作某些核心业务事件(抢单,发单等)时才弹出强制更新。另外可以在从后台切换到前台时弹出,注意要考虑到操作了一半,有电话过来,去接电话了,若弹出强制更新你以前做的事情都泡汤了,所以这种情况就看你的选择了。
第三种实现和场景,在应用的设置里增加版本检测与更新。这种方案已经在2015年3月20日把它放倒了。我们发布的app就是因为这被驳回过。苹果审核时不能有这样的功能。只所以拎出来说说是因为有部分以前发布的应用现在仍旧有这类更新。

手机自动更新关闭,开启就不用再说了吧:
在设置中,左边目录往下啦,看到一个itunes store和app store,选择点击;
点击之后看到自动下载的项目:一、应用程序;二、更新;就是后台联网有更新的情况下自动下载应用程序和更新软件。
我们只要把开关关闭,如果需要更新进入app store选择手动更新即可,这样就不会无缘无故耗费流量了。特别是iphone,有时无意识的开着3g或4g网络(有些区域没有高级基站,信号弱时会自动切换到2g网络),骚包的人从来没有关过蜂窝通信,我是小市民,一般关闭蜂窝通信,需要时再打开。
技术分享

登录时版本更新的部分代码如下:

AppDelegate.mm文件
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    //    [HomeViewController checkNetWork];
    FLDDLogDebug(@"g_versionState :%d, g_bVersionUpdatePrompt : %d", [User currentUser].g_versionState, [User currentUser].g_bVersionUpdatePrompt);

    if((NotReachable == [SelfUser currentSelfUser].networkStatus) && (g_showNoNetNotice))
    {
        FLDDLogDebug(@"applicationDidBecomeActive : g_showNoNetNotice");
        [HomeViewController checkNetWork];
    }

    if((VERSION_STATE_FORCE_UPDATE == [User currentUser].g_versionState) && (!([User currentUser].g_bVersionUpdatePrompt)) && ([User currentUser].g_newVersionURL != nil))
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"版本更新" message:@"检测到最新版本,请及时更新" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil];
        alert.tag = 100;
        @try
        {
            [alert show];
            [User currentUser].g_bVersionUpdatePrompt = YES;
        }
        @catch (NSException *exception) {
        }

    }

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateVersionNotification:) name:UPDATE_VERSION_NOTIFICATION object:nil];
.
.
.
}

- (void)updateVersionNotification:(NSNotification *)notification
{
    [AppDelegate registerForRemoteNotification];

    if((VERSION_STATE_FORCE_UPDATE == [User currentUser].g_versionState) && ([User currentUser].g_newVersionURL != nil))
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"版本更新" message:@"检测到最新版本,请及时更新" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil,nil];
        alert.tag = 100;
        @try
        {
            [alert show];
            [User currentUser].g_bVersionUpdatePrompt = YES;
        }
        @catch (NSException *exception) {
        }
    }
    else if((VERSION_STATE_REMIND_UPDATE == [User currentUser].g_versionState) && ([User currentUser].g_newVersionURL != nil))
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"版本更新" message:@"检测到最新版本,请及时更新" delegate:self cancelButtonTitle:@"放弃" otherButtonTitles:@"更新",nil];
        alert.tag = 101;
        @try
        {
            [alert show];
        }
        @catch (NSException *exception) {
        }
    }
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if([alertView tag] == 100)
    {
        switch (buttonIndex) {
            case 0: //YES应该做的事
            {
                [User currentUser].g_bVersionUpdatePrompt = NO;
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[User currentUser].g_newVersionURL]];
                break;
            }
        }
    }
    else if([alertView tag] == 101)
    {
        switch (buttonIndex) {
            case 0://NO应该做的事
            {
                //[User currentUser].g_bVersionUpdatePrompt = NO;
                [[NSNotificationCenter defaultCenter] postNotificationName:LOGIN_NOTIFICATION object:nil];
                break;
            }
            case 1: //YES应该做的事
            {
                //[User currentUser].g_bVersionUpdatePrompt = NO;
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[User currentUser].g_newVersionURL]];
                break;
            }

        }
    }
}

HomeViewController.m文件
- (void)autoLogin
{
    .
    .
    .
    [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_VERSION_NOTIFICATION object:nil];
    .
    .
    .
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1010)
    {
        _isAlertCherkNet = NO;

        if (_needSendNotice)
        {
            if (self.isHaveNet)
            {
                _needSendNotice = NO;
                _isAlertCherkNet = NO;
                [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_VERSION_NOTIFICATION object:nil];
            }
            else
            {
                if(!_isAlertCherkNet && !_isAlertSrvFail)
                {
                    self.alertView = [[UIAlertView alloc] initWithTitle:nil message:@"当前无网络,请打开网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    self.alertView.tag = 1010;
                    _isAlertCherkNet = YES;
                    [self.alertView  show];
                    self.alertView = nil;
                }
            }

        }
    }
}
LoginViewController.m文件
-(void)btn{
    .
    .
    .
    [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_VERSION_NOTIFICATION object:nil];
    .
    .
    .
}


User.m文件
+ (void)loginWithParams:(NSDictionary *)params block:(void (^)(NSArray *, NSError *))block
{
    NSMutableDictionary *muParams = [NSMutableDictionary dictionaryWithDictionary:params];

    NSString *version = [AppManager getVersion];
    NSString *model = [AppManager getDeviceModel];
    NSString *iOSVersion = [AppManager getIOSVersion];

    [muParams setObject:version forKey:@"version"];
    [muParams setObject:model forKey:@"phoneType"];
    [muParams setObject:iOSVersion forKey:@"phoneSystem"];
    [muParams setObject:g_loginCmd forKey:@"cmdCode"];

    [[API shareAPI] GET:@"loginWithFixedPasswordJsonPhone.htm" params:muParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

//        [[NSNotificationCenter defaultCenter] postNotificationName:REMOVE_LOCALNOTIFICATION_NOTIFICATION object:nil];//登录成功后,取消之前所有不是本用户的催单提醒

        NSString *cookie = [operation.response.allHeaderFields objectForKey:@"Set-Cookie"];//保存Cookie(仅登录成功后保存)
        [AppManager setUserDefaultsValue:cookie key:@"cookie"];

        NSDictionary *responseBody = [responseObject objectForKey:@"body"];
        .
        .
        .
        [User currentUser].grabMessage = [[responseBody objectForKey:@"grabCityNotEqual"] toString];

        [User currentUser].g_newVersionURL = nil;
        NSString *hasNewVersion= [[responseBody objectForKey:@"hasNewVersion"] toString];
        if([hasNewVersion isEqualToString:@"0"])
        {
            [User currentUser].g_versionState = VERSION_STATE_NO_UPDATE;
        }
        else
        {
            NSString *needUpgrade= [[responseBody objectForKey:@"needUpgrade"] toString];
            if([needUpgrade isEqualToString:@"1"])
            {
                [User currentUser].g_versionState = VERSION_STATE_FORCE_UPDATE;
            }
            else
            {
                [User currentUser].g_versionState = VERSION_STATE_REMIND_UPDATE;
            }
            NSString *downloadUrl= [[responseBody objectForKey:@"downloadUrl"] toString];
            [User currentUser].g_newVersionURL = downloadUrl;
        }

        NSArray *districts = [[responseBody objectForKey:@"countyList"] objectForKey:@"rows"];


        NSMutableArray *muArray = [NSMutableArray array];

        for (NSDictionary *attribute in districts) {
            District *district = [[District alloc] initWithAttributes:attribute];
            [muArray addObject:district];
        }

        [User currentUser].districts = [NSArray arrayWithArray:muArray];

        [[User currentUser] saveUserInfo];
//        [AppManager addUserRemindNotification];
//        [AppManager addCurrentOrderRemind];


        if (block) {
            block([NSArray arrayWithArray:muArray], nil);
        }

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

        [User currentUser].state = @"-1";

        if (block) {
            block(@[], error);
        }
    }];

}

+ (void)reLoginWithParams:(NSDictionary *)params block:(void (^)(NSArray *, NSError *))block
{
    NSMutableDictionary *muParams = [NSMutableDictionary dictionaryWithDictionary:params];

    NSString *version = [AppManager getVersion];
    NSString *model = [AppManager getDeviceModel];
    NSString *iOSVersion = [AppManager getIOSVersion];

    [muParams setObject:version forKey:@"version"];
    [muParams setObject:model forKey:@"phoneType"];
    [muParams setObject:iOSVersion forKey:@"phoneSystem"];
    [muParams setObject:g_reloginCmd forKey:@"cmdCode"];
    [AppManager setUserDefaultsValue:@"" key:@"cookie"];

    [[API shareAPI] GET:@"reLoginWithFixedPasswordJsonPhone.htm" params:muParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

//        [[NSNotificationCenter defaultCenter] postNotificationName:REMOVE_LOCALNOTIFICATION_NOTIFICATION object:nil];//登录成功后,取消之前所有不是本用户的催单提醒

        NSString *cookie = [operation.response.allHeaderFields objectForKey:@"Set-Cookie"];//保存Cookie(仅登录成功后保存)

        FLDDLogDebug(@"login cookie\n ----------\n%@\n------------\n", cookie);
        [AppManager setUserDefaultsValue:cookie key:@"cookie"];

        NSDictionary *responseBody = [responseObject objectForKey:@"body"];
        .
        .
        .
        [User currentUser].g_newVersionURL = nil;
        NSString *hasNewVersion= [[responseBody objectForKey:@"hasNewVersion"] toString];
        if([hasNewVersion isEqualToString:@"0"])
        {
            [User currentUser].g_versionState = VERSION_STATE_NO_UPDATE;
        }
        else
        {
            NSString *needUpgrade= [[responseBody objectForKey:@"needUpgrade"] toString];
            if([needUpgrade isEqualToString:@"1"])
            {
                [User currentUser].g_versionState = VERSION_STATE_FORCE_UPDATE;
            }
            else
            {
                [User currentUser].g_versionState = VERSION_STATE_REMIND_UPDATE;
            }
            NSString *downloadUrl= [[responseBody objectForKey:@"downloadUrl"] toString];
            [User currentUser].g_newVersionURL = downloadUrl;
        }

        NSArray *districts = [[responseBody objectForKey:@"countyList"] objectForKey:@"rows"];


        NSMutableArray *muArray = [NSMutableArray array];

        for (NSDictionary *attribute in districts) {
            District *district = [[District alloc] initWithAttributes:attribute];
            [muArray addObject:district];
        }

        [User currentUser].districts = [NSArray arrayWithArray:muArray];

        for (NSDictionary *attribute in districts) {
            District *district = [[District alloc] initWithAttributes:attribute];
            [muArray addObject:district];
        }

        [[User currentUser] saveUserInfo];
//        [AppManager addUserRemindNotification];
//        [AppManager addCurrentOrderRemind];

        if (block) {
            block([NSArray arrayWithArray:muArray], nil);

        }

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

        [User currentUser].state = @"-1";

        if (block) {
            block(@[], error);
        }
    }];
}

第二种场景,运行版本更新部分代码:

AppDelegate.mm文件
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    //    [application setApplicationIconBadgeNumber:[[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] intValue]];
    [application setApplicationIconBadgeNumber:0];
    //    FLDDLogDebug(@"Receive Notify: %@", [userInfo JSONString]);
    FLDDLogDebug(@"%@",userInfo);
    NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    self.dictPush = nil;
    self.dictPush  =[NSDictionary dictionaryWithDictionary:[userInfo objectForKey:@"page"] ];

    NSString* sreType =[self.dictPush valueForKey:@"type"];
    if (![[[self.dictPush valueForKey:@"userTel"] toString]isEqualToString:[AppManager valueForKey:@"telephone"]]) {
        return;
    }

    if((sreType) && [sreType isEqualToString:@"mddy_needUpgrade"])
    {
        NSString *url = [self.dictPush objectForKey:@"downloadUrl"];
        if(url != nil)
        {
            g_needUpgrade = 1;
            g_downloadUrl = url;
        }
        return;
    }
.
.
.
}

HomeViewController.m文件
#pragma mark发单
-(void)buttonSendPressed{
    [self hiddenDetail];
    if (NotReachable == [SelfUser currentSelfUser].networkStatus) {
        [MBProgressHUD hudShowWithStatus:self :@"当前网络不可用,请检查您的网络设置"];
        return;
    }
    if((g_needUpgrade == 1) && (g_downloadUrl != nil))
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_HEART_VERSION_NOTIFICATION object:nil];
        return;
    }
    .
    .
    .
}


-(void)buttonSoonSendPressed{
    [self hiddenDetail];
    if (NotReachable == [SelfUser currentSelfUser].networkStatus) {
        [MBProgressHUD hudShowWithStatus:self :@"当前网络不可用,请检查您的网络设置"];
        return;
    }

    if(g_loginStat == LOGIN_STATE_LOGIN_SUCESS)
    {
        if (self.remainOrder == 0) {
            [MBProgressHUD hudShowWithStatus:self :@"抱歉,您今日发单量已达上限,明天再来吧!"];
            return;
        }

        if (self.remainQuickOrder == 0) {
            [MBProgressHUD hudShowWithStatus:self :@"抱歉,您今日极速发单量已达上限,明天再来吧!"];
            return;
        }
    }
    else
    {
        [MBProgressHUD hudShowWithStatus:self :@"网络比蜗牛还慢,挤不进去呀"];
        [[NSNotificationCenter defaultCenter]postNotificationName:ONCELOGININ object:self];
        return;
    }



    if((g_needUpgrade == 1) && (g_downloadUrl != nil))
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_HEART_VERSION_NOTIFICATION object:nil];
        return;
    }
    .
    .
    .
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

app的版本更新

标签:app版本更新   登录时版本更新   运行版本更新   

原文地址:http://blog.csdn.net/jia12216/article/details/47018463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!