gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:38:12 25 4
gpt4 key购买 nike

我有一个简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com