0

I am trying to do a MUC on iOS using xmpp_messenger_ios & XMPPFramework

Here is the code to join the room.

   func createOrJoinRoomOnXMPP(){
    // location has named array of lat and long

    NSLog("Creating room on XMPP")

    let roomJID: XMPPJID = XMPPJID.jidWithString(self.roomID + "@conference.ip-172-31-41-100")

    let roomData: XMPPRoomCoreDataStorage = XMPPRoomCoreDataStorage.sharedInstance()

    let chatRoom = XMPPRoom.init(roomStorage: roomData, jid: roomJID, dispatchQueue: dispatch_get_main_queue())

    chatRoom.activate(OneChat.sharedInstance.xmppStream)
    chatRoom.addDelegate(self, delegateQueue: dispatch_get_main_queue())

    //        let history = DDXMLElement.elementWithName("history")
    //        // Get lst messegs of the room
    //        history.addAttributeWithName("maxstanzas", stringValue: "10")

    chatRoom.joinRoomUsingNickname(OneChat.sharedInstance.xmppStream!.myJID.user, history: nil)
}

as soon as this block executes I get an error in this code:

extension OneMessage: XMPPStreamDelegate {

public func xmppStream(sender: XMPPStream, didSendMessage message: XMPPMessage) {
    if let completion = OneMessage.sharedInstance.didSendMessageCompletionBlock {
        completion(stream: sender, message: message)
    }
    //OneMessage.sharedInstance.didSendMessageCompletionBlock!(stream: sender, message: message)
}

public func xmppStream(sender: XMPPStream, didReceiveMessage message: XMPPMessage) {
    let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())

    if !OneChats.knownUserForJid(jidStr: user.jidStr) { // <<< ERROR LINE
        OneChats.addUserToChatList(jidStr: user.jidStr)
    }

    if message.isChatMessageWithBody() {
        OneMessage.sharedInstance.delegate?.oneStream(sender, didReceiveMessage: message, from: user)
    } else {
        //was composing
        if let _ = message.elementForName("composing") {
            OneMessage.sharedInstance.delegate?.oneStream(sender, userIsComposing: user)
        }
    }
}

}

fatal error: unexpectedly found nil while unwrapping an Optional value

I have noticed that as soon as the connection is made to chat room it fetches previous messages, and thus the above code is executed.

Please help me out is doing a MUC for room chat on ios. I have searched and have not found any solution.

thanks

3 Answers3

0

I solved this by this temporary solution.

extension OneMessage: XMPPStreamDelegate {

    public func xmppStream(sender: XMPPStream, didSendMessage message: XMPPMessage) {
        if let completion = OneMessage.sharedInstance.didSendMessageCompletionBlock {
            completion(stream: sender, message: message)
        }
        //OneMessage.sharedInstance.didSendMessageCompletionBlock!(stream: sender, message: message)
    }

    public func xmppStream(sender: XMPPStream, didReceiveMessage message: XMPPMessage) {
        NSLog("This is blocked")

//      let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())
//      
//      if !OneChats.knownUserForJid(jidStr: user.jidStr) {
//          OneChats.addUserToChatList(jidStr: user.jidStr)
//      }
//      
//      if message.isChatMessageWithBody() {
//          OneMessage.sharedInstance.delegate?.oneStream(sender, didReceiveMessage: message, from: user)
//      } else {
//          //was composing
//          if let _ = message.elementForName("composing") {
//              OneMessage.sharedInstance.delegate?.oneStream(sender, userIsComposing: user)
//          }
//      }
    }
}

Blocking the OneMessage.swift code.

and handling the incoming messages in my ViewController.

This is not the right way to do it. but until ProcessOne give support for MUC this can be done.

  • If anyone finds a better solution then please post it here. – ramit wadhwa Sep 09 '16 at 05:01
  • but by doing this it affects the single chat. If you send message in single chat the message only you can see after refreshing the single chat – PRADIP KUMAR Nov 18 '16 at 06:00
  • In xmpp-messenger-ios already oneroom class is there .How can i use it for my purpose. I am not able to use that class . if you have any idea let me know.. – PRADIP KUMAR Nov 18 '16 at 06:02
0

Unwrapping that causes nil happens on:

  • user (return value of userForJID method is XMPPUserCoreDataStorageObject! )

  • jidStr (the type is String!)

Investigate which one happens to be nil.

Possible causes of user to be nil - Nil value of jid or managedObjectContext is used in userForJID(:xmppStream:managedObjectContext)`

To find out which one is nil, simply do this:

guard let user = OneChat.sharedInstance.xmppRosterStorage.userForJID(message.from(), xmppStream: OneChat.sharedInstance.xmppStream, managedObjectContext: OneRoster.sharedInstance.managedObjectContext_roster())
else { fatalError("user is nil") }

guard let userJIDStr = user.jidStr
else { fatalError("jidStr is nil") }
Fredric
  • 1,089
  • 2
  • 10
  • 19
0

I think you need to understand XMPP MUC first, read this doc.

When you send a message to MUCRoom, the serve will broadcast the message to all the members, including yourself.

And here message.from() = room.jid BUT NOT user.jid.

That's why the user you tried to get from roster is nil.

dichen
  • 1,643
  • 14
  • 19