SendBird 聊天 SDK Rubymotion 集成
SendBird chat SDK Rubymotion integration
我正在尝试将 SendBird SDK 聊天与 Rubymotion 集成(通过其 CocoaPod)。
初始步骤工作正常,我已经能够将 pod 添加到我的 Rakefile(使用 motion-cocoapods)并编译没有问题,登录用户并创建消息通道。然而,事件处理程序让我难住了。
这些是我能够从 SendBird 文档翻译的步骤:
在应用委托中初始化
SendBird Objective C:
#import <SendBirdSDK/SendBirdSDK.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
NSString *APP_ID = @"<YOUR_APP_ID>"; // You could get it from SendBird Dashboard.
[SendBird initAppId:APP_ID];
// ...
return YES;
}
Rubymotion:
APP_ID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
SendBird.initAppId(APP_ID)
在我的应用程序中登录/注册时登录 SendBird
SendBird Objective C:
- (void)viewDidLoad
{
// other setup code
[SendBird loginWithUserId:USER_ID andUserName:USER_NICKNAME];
}
Rubymotion:
(登录后我将用户信息存储在 Auth.current_user 哈希中)
SendBird.loginWithUserId(Auth.current_user["id"],andUserName:Auth.current_user["name"])
创建消息通道并设置事件处理程序
SendBird Objective C:
[SendBird startMessagingWithUserId:targetUserId]; // 1 on 1 channel
// In your event handler
[SendBird setEventHandlerConnectBlock:^(SendBirdChannel *channel) {
// Callback for [SendBird connectWithTS:] or [SendBird connect:]
} errorBlock:^(NSInteger code) {
// Error occured due to bad APP_ID (or other unknown reason)
} channelLeftBlock:^(SendBirdChannel *channel) {
// Callback for [SendBird leaveChannel:]
} messageReceivedBlock:^(SendBirdMessage *message) {
// Received a regular chat message
} fileReceivedBlock:^(SendBirdFileLink *fileLink) {
// Received a file
} messagingStartedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird startMessagingWithUserId:] or [SendBird inviteMessagingWithChannelUrl:]
} messagingUpdatedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird inviteMessagingWithChannelUrl:]
} messagingEndedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird endMessagingWithChannelUrl:]
} allMessagingEndedBlock:^ {
// Callback for [SendBird endAllMessaging:]
} readReceivedBlock:^(SendBirdReadStatus *status) {
// When ReadStatus has been received
} messageDeliveryBlock:^(BOOL send, NSString *message, NSString *data, NSString *tempId{
// To determine the message has been successfully sent.
} mutedMessagesReceivedBlock:^(SendBirdMessage *message) {
// When soft-muted messages have been received
} mutedFileReceivedBlock:^(SendBirdFileLink *message) {
// When soft-muted files have been received
}];
Rubymotion:
我遇到麻烦了。我能够创建消息通道,但我不知道如何在 Rubymotion 中编写 Objective-C 事件处理程序。 http://objc2rubymotion.herokuapp.com/ 的在线翻译器因 ^ 和 * 语法而失败。感谢任何帮助。
SendBird.startMessagingWithUserId(id_of_target_user)
#Event Handler
编辑:
一位比我更了解 Rubymotion 的朋友为我指出了正确的方向,所以现在我有了以下事件处理程序:
SendBird.startMessagingWithUserId(2)
SendBird.setEventHandlerConnectBlock(
-> (channel) {
# handle connect
},
messagingStartedBlock: -> (channel) {
mp "channel created:" + channel.to_s
})
然而,这失败了,出现了 NoMethodError:
2016-08-03 19:48:58.711 faces_ios_v1[75673:798798] chat_screen.rb:24:in `on_load': undefined method `setEventHandlerConnectBlock:messagingStartedBlock:' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
2016-08-03 19:48:58.723 faces_ios_v1[75673:798798] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'chat_screen.rb:24:in `on_load': undefined method `setEventHandlerConnectBlock:messagingStartedBlock:' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
如果我不在事件处理程序中包含 messagingStartedBlock
,我会收到以下错误:
2016-08-03 19:35:20.120 faces_ios_v1[74958:793380] chat_screen.rb:14:in `on_load': undefined method `setEventHandlerConnectBlock' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
2016-08-03 19:35:20.132 faces_ios_v1[74958:793380] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'chat_screen.rb:14:in `on_load': undefined method `setEventHandlerConnectBlock' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
这没有意义,因为 setEventHandlerConnectBlock
显然应该是 SendBird class 的一个方法。
相关的SendBird SDK文档可以在这里找到:
好的,在朋友的大力帮助下解决了这个问题。
事实证明,SendBird 事件处理程序方法需要传递所有事件块,否则将无法工作。所以这会起作用:
SendBird.startMessagingWithUserId(2)
SendBird.setEventHandlerConnectBlock(
-> (channel) { },
errorBlock: -> (code) { },
channelLeftBlock: -> (channel) { },
messageReceivedBlock: -> (message) { },
systemMessageReceivedBlock: -> (message) { },
broadcastMessageReceivedBlock: -> (message) { },
fileReceivedBlock: -> (file_link) { },
messagingStartedBlock: -> (channel) {
mp "Messaging started"
mp channel
},
messagingUpdatedBlock: -> (channel) { },
messagingEndedBlock: -> (channel) { },
allMessagingEndedBlock: -> { },
messagingHiddenBlock: -> (channel) { },
allMessagingHiddenBlock: -> { },
readReceivedBlock: -> (status) { },
typeStartReceivedBlock: -> (status) { },
typeEndReceivedBlock: -> (status) { },
allDataReceivedBlock: -> (sendBirdDataType, count) { },
messageDeliveryBlock: -> (send, message, data, temp_id) { },
mutedMessagesReceivedBlock: -> (message) { },
mutedFileReceivedBlock: -> (message) { }
)
并将输出:
"Messaging started"
#<SendBirdMessagingChannel:0x10f9bcdc0>
此集成工作后,我将上传一个教程/github 项目。
完成后我会post回复link作为评论
我正在尝试将 SendBird SDK 聊天与 Rubymotion 集成(通过其 CocoaPod)。
初始步骤工作正常,我已经能够将 pod 添加到我的 Rakefile(使用 motion-cocoapods)并编译没有问题,登录用户并创建消息通道。然而,事件处理程序让我难住了。
这些是我能够从 SendBird 文档翻译的步骤:
在应用委托中初始化
SendBird Objective C:
#import <SendBirdSDK/SendBirdSDK.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// ...
NSString *APP_ID = @"<YOUR_APP_ID>"; // You could get it from SendBird Dashboard.
[SendBird initAppId:APP_ID];
// ...
return YES;
}
Rubymotion:
APP_ID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx"
SendBird.initAppId(APP_ID)
在我的应用程序中登录/注册时登录 SendBird
SendBird Objective C:
- (void)viewDidLoad
{
// other setup code
[SendBird loginWithUserId:USER_ID andUserName:USER_NICKNAME];
}
Rubymotion:
(登录后我将用户信息存储在 Auth.current_user 哈希中)
SendBird.loginWithUserId(Auth.current_user["id"],andUserName:Auth.current_user["name"])
创建消息通道并设置事件处理程序
SendBird Objective C:
[SendBird startMessagingWithUserId:targetUserId]; // 1 on 1 channel
// In your event handler
[SendBird setEventHandlerConnectBlock:^(SendBirdChannel *channel) {
// Callback for [SendBird connectWithTS:] or [SendBird connect:]
} errorBlock:^(NSInteger code) {
// Error occured due to bad APP_ID (or other unknown reason)
} channelLeftBlock:^(SendBirdChannel *channel) {
// Callback for [SendBird leaveChannel:]
} messageReceivedBlock:^(SendBirdMessage *message) {
// Received a regular chat message
} fileReceivedBlock:^(SendBirdFileLink *fileLink) {
// Received a file
} messagingStartedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird startMessagingWithUserId:] or [SendBird inviteMessagingWithChannelUrl:]
} messagingUpdatedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird inviteMessagingWithChannelUrl:]
} messagingEndedBlock:^(SendBirdMessagingChannel *channel) {
// Callback for [SendBird endMessagingWithChannelUrl:]
} allMessagingEndedBlock:^ {
// Callback for [SendBird endAllMessaging:]
} readReceivedBlock:^(SendBirdReadStatus *status) {
// When ReadStatus has been received
} messageDeliveryBlock:^(BOOL send, NSString *message, NSString *data, NSString *tempId{
// To determine the message has been successfully sent.
} mutedMessagesReceivedBlock:^(SendBirdMessage *message) {
// When soft-muted messages have been received
} mutedFileReceivedBlock:^(SendBirdFileLink *message) {
// When soft-muted files have been received
}];
Rubymotion:
我遇到麻烦了。我能够创建消息通道,但我不知道如何在 Rubymotion 中编写 Objective-C 事件处理程序。 http://objc2rubymotion.herokuapp.com/ 的在线翻译器因 ^ 和 * 语法而失败。感谢任何帮助。
SendBird.startMessagingWithUserId(id_of_target_user)
#Event Handler
编辑:
一位比我更了解 Rubymotion 的朋友为我指出了正确的方向,所以现在我有了以下事件处理程序:
SendBird.startMessagingWithUserId(2)
SendBird.setEventHandlerConnectBlock(
-> (channel) {
# handle connect
},
messagingStartedBlock: -> (channel) {
mp "channel created:" + channel.to_s
})
然而,这失败了,出现了 NoMethodError:
2016-08-03 19:48:58.711 faces_ios_v1[75673:798798] chat_screen.rb:24:in `on_load': undefined method `setEventHandlerConnectBlock:messagingStartedBlock:' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
2016-08-03 19:48:58.723 faces_ios_v1[75673:798798] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'chat_screen.rb:24:in `on_load': undefined method `setEventHandlerConnectBlock:messagingStartedBlock:' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
如果我不在事件处理程序中包含 messagingStartedBlock
,我会收到以下错误:
2016-08-03 19:35:20.120 faces_ios_v1[74958:793380] chat_screen.rb:14:in `on_load': undefined method `setEventHandlerConnectBlock' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
2016-08-03 19:35:20.132 faces_ios_v1[74958:793380] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'chat_screen.rb:14:in `on_load': undefined method `setEventHandlerConnectBlock' for SendBird:Class (NoMethodError)
from screen_module.rb:39:in `view_did_load'
from view_controller.rb:15:in `viewDidLoad'
这没有意义,因为 setEventHandlerConnectBlock
显然应该是 SendBird class 的一个方法。
相关的SendBird SDK文档可以在这里找到:
好的,在朋友的大力帮助下解决了这个问题。
事实证明,SendBird 事件处理程序方法需要传递所有事件块,否则将无法工作。所以这会起作用:
SendBird.startMessagingWithUserId(2)
SendBird.setEventHandlerConnectBlock(
-> (channel) { },
errorBlock: -> (code) { },
channelLeftBlock: -> (channel) { },
messageReceivedBlock: -> (message) { },
systemMessageReceivedBlock: -> (message) { },
broadcastMessageReceivedBlock: -> (message) { },
fileReceivedBlock: -> (file_link) { },
messagingStartedBlock: -> (channel) {
mp "Messaging started"
mp channel
},
messagingUpdatedBlock: -> (channel) { },
messagingEndedBlock: -> (channel) { },
allMessagingEndedBlock: -> { },
messagingHiddenBlock: -> (channel) { },
allMessagingHiddenBlock: -> { },
readReceivedBlock: -> (status) { },
typeStartReceivedBlock: -> (status) { },
typeEndReceivedBlock: -> (status) { },
allDataReceivedBlock: -> (sendBirdDataType, count) { },
messageDeliveryBlock: -> (send, message, data, temp_id) { },
mutedMessagesReceivedBlock: -> (message) { },
mutedFileReceivedBlock: -> (message) { }
)
并将输出:
"Messaging started"
#<SendBirdMessagingChannel:0x10f9bcdc0>
此集成工作后,我将上传一个教程/github 项目。
完成后我会post回复link作为评论