ios - QuickBlox 视频聊天 : QBRequest. logInWithUserEmail 与 QBChat.instance().connectWithUser

标签 ios swift webrtc quickblox

我有一个简单的 QuickBlox 聊天应用程序,它是按照 iOS 教程构建的:

http://quickblox.com/developers/Sample-webrtc-ios#Sources

我已成功创建用户并登录。但是,当我尝试启动 session 时遇到错误:“您必须登录才能使用聊天 API”。

    let newSession: QBRTCSession = QBRTCClient.instance().createNewSessionWithOpponents(["12498970"], withConferenceType: QBRTCConferenceType.Video)

我可以通过在每次打开它时添加 QBChat.instance().connectWithUser 来解决这个问题:

    QBChat.instance().connectWithUser(user!) { (error) in
        if error != nil {
            print("error: \(error)")
        }
        else {
            print("login to chat succeeded")
        }
    }

但不知何故,这看起来很奇怪,因为每次打开应用程序时我都必须缓存密码或提示用户输入密码。 QBSession.currentSession().currentUser 仍然有效,但 QBChat 用户已失效,这似乎很奇怪。实现这一目标的最佳做法是什么?在所有示例中,密码都是硬编码的。这似乎不是一个很好的解决方案。

最佳答案

我最终遵循了 Q-municate 中的示例,这是 Quickblox 人员构建的应用程序,主要用于演示他们的整个程序包,并为您的聊天需求提供实际解决方案。我还有一些其他自定义内容,不需要很多功能,所以我仍在尝试深入了解他们如何实现它的细节。 Q-municate 链接:

http://quickblox.com/developers/Q-municate#1._Get_the_source_code .

在他们的登录流程中,他们使用为 Q-municate 编写的 QMApi 模块:

    [[QMApi instance] loginWithEmail:email
                            password:password
                          rememberMe:weakSelf.rememberMeSwitch.on
                          completion:^(BOOL success)
     {
         [SVProgressHUD dismiss];

         if (success) {
             [[QMApi instance] setAutoLogin:weakSelf.rememberMeSwitch.on
                            withAccountType:QMAccountTypeEmail];
             [weakSelf performSegueWithIdentifier:kTabBarSegueIdnetifier
                                           sender:nil];
         }
     }];

在 loginWithEmail 中,他们的 settingsManager 缓存了这个登录:

            [weakSelf.settingsManager setLogin:email andPassword:password];

这实际上只是一种在 SSKeyChain 中缓存密码的方法。

[SSKeychain setPassword:password forService:kQMAuthServiceKey account:login];

稍后,当您返回应用程序时,他们会调用自动登录:

if (!self.isAuthorized) {
    if (self.settingsManager.accountType == QMAccountTypeEmail && self.settingsManager.password && self.settingsManager.login) {

        NSString *email = self.settingsManager.login;
        NSString *password = self.settingsManager.password;

        [self loginWithEmail:email password:password rememberMe:YES completion:completion];
    }
    else if (self.settingsManager.accountType == QMAccountTypeFacebook) {

        [self loginWithFacebook:completion];
    }
    else {

        if (completion) completion(NO);
    }
}
else {
    if (completion) completion(YES);
}

self.settingsManager.password 从 SSKeychain 中提取密码的地方:

NSString *password = [SSKeychain passwordForService:kQMAuthServiceKey account:self.login];

autoLogin 在加载主聊天选项卡时调用。这就是我们对 connectToChat 的经典调用:

[[QMApi instance] autoLogin:^(BOOL success) {
    if (!success) {

        [[QMApi instance] logoutWithCompletion:^(BOOL succeed) {
            //
            [weakSelf performSegueWithIdentifier:@"SplashSegue" sender:nil];
        }];

    } else {

        // subscribe to push notifications
        [[QMApi instance] subscribeToPushNotificationsForceSettings:NO complete:^(BOOL subscribeToPushNotificationsSuccess) {

            if (!subscribeToPushNotificationsSuccess) {
                [QMApi instance].settingsManager.pushNotificationsEnabled = NO;
            }
        }];

        [weakSelf connectToChat];
    }
}];

因此,从技术上讲,每次打开应用程序并且不再连接聊天时,文档都会通过登录聊天来做正确的事情。只是有一种更复杂但更安全的方法来存储该密码,因此用户不必重新输入它。

TLDR:它在我的代码中(和 swift 中)的工作方式是:

登录时:

    QBRequest.logInWithUserEmail(email, password: password, successBlock: { (response, user) in
        SSKeychain.setPassword(password, forService: "kMyAppLoginServiceKey", account: email)

        }) { (errorResponse) in
            print("Error: \(errorResponse)")
            self.simpleAlert("Could not log in", defaultMessage: nil,  error: nil)
    }

每当聊天 View 加载时:

    if !QBChat.instance().isConnected() {
        QBRTCClient.initializeRTC()
        QBRTCClient.instance().addDelegate(self)

        let user = QBSession.currentSession().currentUser
        let password = SSKeychain.passwordForService("kMyAppLoginServiceKey", account: user?.email!)
        user!.password = password
        QBChat.instance().addDelegate(self)
        QBChat.instance().connectWithUser(user!) { (error) in
            if error != nil {
                print("error: \(error)")
            }
            else {
                print("login to chat succeeded")
            }
        }
    }

关于ios - QuickBlox 视频聊天 : QBRequest. logInWithUserEmail 与 QBChat.instance().connectWithUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126690/

相关文章:

ios - 将导航栏设置为与 iOS 7 上的深色键盘相同的颜色

webrtc - 我们可以将 google stun 服务器(nodejs webrtc.io-client 的默认设置)用于商业应用程序吗?

ios - 无法在侧边菜单中进行 segue

ios - 不鼓励的配置 : Value other than autosizing specified for width (System items, 除固定空间外,应使用自动调整大小)

ios - 解析目标 - C 到 Swift iOS

Swift tableView 滚动到行为我的 tableview 的内容偏移值提供了错误的值

swift - 当没有真正的 "else"案例涵盖所有内容时,我如何根据特定条件最好地处理返回特定对象?

node.js - 设置Nodejs WebRTC视频通话,并且Turn/ICE失败,CORS被阻止

multithreading - 如何在iOS中正确关闭WebRTC peerConnection?

ios - IOS Safari 上的实习生 3.4.1 "unable to set accept insecure certs on Safari"