SendBird聊天SDK Rubymotion集成

时间:2016-08-03 14:58:43

标签: rubymotion sendbird

我正在尝试将SendBird SDK聊天与Rubymotion(通过其CocoaPod)集成。

初始步骤工作正常,我已经能够将pod添加到我的Rakefile(使用motion-cocoapods)并编译没有问题,登录用户并创建消息传递通道。但是,事件处理程序让我感到难过。

这些是我能够从SendBird文档中翻译的步骤:

在app delegate中初始化

SendBird目标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目标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目标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类的一种方法。

可以在此处找到相关的SendBird SDK文档:

https://docs.sendbird.com/ios#messaging_channel

1 个答案:

答案 0 :(得分:0)

好的,这是在朋友的大量帮助下解决的。

事实证明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>

一旦我开始使用此集成,我将上传一个tutorial / github项目。

一旦完成,我会将链接作为评论回复