如何在XMPP中向其他用户发送位置? Swift 3.0

时间:2017-08-31 13:32:48

标签: ios location xmpp chat xmppframework

我正在开发聊天应用,因为我需要发送位置其他用户。 (一对一聊天) 我已阅读xep-0080但在XMPP框架中XEP-80类无效。 我还检查了XMPPPubSub模块,但没有得到如何将用户位置发送给其他用户。

参考链接:

  1. https://github.com/robbiehanson/XMPPFramework/issues/506
  2. How to pass location using XMPP in ios sdk?
  3. https://github.com/buddycloud/buddycloud-iOS-client
  4. 服务器:ejabber

    如果提供代码片段和教程链接,那将是完整的帮助。

1 个答案:

答案 0 :(得分:0)

经过多次尝试后,我已成功将用户自定义位置发送给其他聊天用户。

已使用的扩展程序:xep-0080

下面我提到发送位置功能

public class func sendLocationMessage(msg:String,lat : String ,long : String ,to receiver: String,completionHandler completion:@escaping XMPPMessageMngCompletionHandler){
    let body = DDXMLElement.element(withName: "body") as! DDXMLElement
    let messageID = XMPPConnect.sharedInstance.xmppStream.generateUUID()
    body.stringValue = "Location"
    let completeMessage = DDXMLElement.element(withName: "message") as! DDXMLElement
    let reuestElemetn = DDXMLElement.element(withName: "request", stringValue: "urn:xmpp:receipts")
    completeMessage.addChild(reuestElemetn as! DDXMLNode)
    completeMessage.addAttribute(withName: "id", stringValue: messageID!)
    completeMessage.addAttribute(withName: "type", stringValue: "chat")
    completeMessage.addAttribute(withName: "to", stringValue: receiver)
    completeMessage.addChild(body)

    let geoElemetn = DDXMLElement.element(withName: "geoloc") as! DDXMLElement
    geoElemetn.addAttribute(withName: "xmlns", stringValue: "http://jabber.org/protocol/geoloc")

    let latElement = DDXMLElement.element(withName: "lat") as! DDXMLElement
    latElement.stringValue = lat
    geoElemetn.addChild(latElement);

    let lngElement = DDXMLElement.element(withName: "lon") as! DDXMLElement
    lngElement.stringValue = long
    geoElemetn.addChild(lngElement);

    let uriElement = DDXMLElement.element(withName: "uri") as! DDXMLElement
    uriElement.stringValue = msg; //google map image url.

    geoElemetn.addChild(uriElement)

    completeMessage.addChild(geoElemetn)

    sharedInstance.didSendMessageCompletionBlock = completion
    XMPPConnect.sharedInstance.xmppStream?.send(completeMessage)
}

通过此功能,您还可以将位置发送到Android(SMACK Lib)

对于 didReceiveMessage 委托方法,您可以检查属性。

   if message.attribute(forName: "geoloc") != nil {
       self.receivedLocationMsgFromUser(message: message, from: from)
   }else{
      self.receivedTextMsgFromUser(message: message, msgStr: msg, from: from)
  }