Search code examples
iosswiftxmppframework

How to check if a user JID is already taken in XMPPFrameWork


I am kind of new to xmppframework. I have a quick question. Is it possible to know if a certain JID is available or no. In other words is it possible to check that the username is already associated or registered with certain account.


Solution

  • with jabber search yes you can search and find user with specific jID or userName or ...

    in here i create function that take some jID as a parameter and the server response to you as a query and inside that query if anythings match there will be items that contains user with matching jID

    func xmppQueryForSearchPacket(forJID jid: String) {
        let searchingServer = "search.something" //something must replace with your xmpp domain name
        let query = DDXMLElement(name: "query", xmlns: "jabber:iq:search")
        let x = DDXMLElement(name: "x", xmlns: "jabber:x:data")
        x.addAttribute(withName: "type", stringValue: "submit")
    
    
        let field1 = DDXMLElement(name: "field")
        let value1 = DDXMLElement(name: "value", stringValue: "jabber:iq:search")
        field1.addAttribute(withName: "type", stringValue: "hidden")
        field1.addAttribute(withName: "var", stringValue: "FORM_TYPE")
        field1.addChild(value1)
    
    
        let field2 = DDXMLElement(name: "field")
        let value2 = DDXMLElement(name: "value", stringValue: jid)
        field2.addAttribute(withName: "type", stringValue: "text-single")
        field2.addAttribute(withName: "var", stringValue: "search")
        field2.addChild(value2)
    
        //we just search in userNames
    
        let field3 = DDXMLElement(name: "field")
        let value3 = DDXMLElement(name: "value", stringValue: "1")
        field3.addAttribute(withName: "type", stringValue: "boolean")
        field3.addAttribute(withName: "var", stringValue: "Username")
        field3.addChild(value3)
    
    
        x.addChild(field1)
        x.addChild(field2)
        x.addChild(field3)
        query.addChild(x)
    
    
        let iq = XMPPIQ(iqType: .set, to: XMPPJID(string: searchingServer), elementID: XMPPStream.generateUUID, child: query)
       XMPPStream.send(iq)
    }
    

    the answer of that packet will come in a query form and you can receive it like this :

    extension yourClassName: XMPPStreamDelegate {
        func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
            let searchingServer = "search.something"
            if iq.from?.bare != searchingServer {
                return true
            }
            return false
        }
    }
    

    for more information please reade XEP-0055.