如何管理Quick Blox会话?

时间:2014-12-22 07:11:33

标签: ios objective-c session quickblox

我有关于Quick blox API的问题。现在我使用以下代码注册用户。

 [QBRequest createSessionWithSuccessBlock:^(QBResponse *response, QBASession *session) {
            // session created

            QBUUser *user = [QBUUser user];
            user.password = userPasswordTextField.text;
            user.login = userNameTextField.text;
            user.fullName = userRealNameTextField.text;
            user.email = userEmailTextField.text;

            // Registration sign up of User
            [QBRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {

                [QBRequest createSessionWithSuccessBlock:^(QBResponse *response, QBASession *session) {
                    NSLog(@"checkingl registering");
                    [QBRequest userWithLogin:user.login successBlock:^(QBResponse *response, QBUUser *user) {
                        NSLog(@"checkingl updatingqb");

                    } errorBlock:^(QBResponse *response) {
                        // Handle error
                    }];

                }errorBlock:^(QBResponse *response) {
                    // Handle error
                }];

            } errorBlock:^(QBResponse *response) {
                // Handle error here
                NSLog(@"error while signing up with QB");

                NSLog(@"fail sign Up %@",response);;

                [self showAlert:nil message:@"User with login that has already been taken" cancelButtonTitle:nil otherButtonTitle:@"OK"];


                return ;

            }];
        } errorBlock:^(QBResponse *response) {
            // handle errors
            NSLog(@" error in creating session %@", response.error);
        }];

在上面的代码中我首先创建一个会话并注册一个新用户,然后我是登录用户。在注册时我在注册时没有登录用户进行QuickBlox聊天,但是稍后当我将在另一个模块中登录Quick blox聊天时,我仍然需要创建新会话或者我必须维护新会话。

1 个答案:

答案 0 :(得分:4)

任何会话在最后一次请求QuickBlox后的2小时内仍然有效。要查看会话的到期日期,请使用下一段代码:

NSDate *sessionExpiratioDate = [QBBaseModule sharedModule].tokenExpirationDate;
NSDate *currentDate = [NSDate date];
NSTimeInterval interval = [currentDate timeIntervalSinceDate:tokenExpirationDate];
if(interval > 0){
  // recreate session here
}

查看this指南。自1.8 iOS SDK起,This功能可用。

参考:Igor Khomenko