简体   繁体   English

如何在XMPP中将位置发送给其他用户? 斯威夫特3.0

[英]How to send location to other user in XMPP ? Swift 3.0

I am developing chat app in that i need to send location other user. 我正在开发聊天应用程序,因为我需要向其他用户发送位置信息。 (One to One Chat) I have read xep-0080 but in XMPP framework XEP-80 class not avail-be. (一对一聊天)我已阅读xep-0080,但在XMPP框架XEP-80类中不可用。 I have also checked XMPPPubSub Module but not getting how to send user location to other user. 我还检查了XMPPPubSub模块,但没有得到如何将用户位置发送给其他用户的信息。

Reference links : 参考链接:

  1. https://github.com/robbiehanson/XMPPFramework/issues/506 https://github.com/robbiehanson/XMPPFramework/issues/506
  2. How to pass location using XMPP in ios sdk? 如何在iOS SDK中使用XMPP传递位置信息?
  3. https://github.com/buddycloud/buddycloud-iOS-client https://github.com/buddycloud/buddycloud-iOS-client

Server : ejabber 服务器:ejabber

It's help full if provide snippet of code and tutorial link. 如果提供代码段和教程链接,将对您有所帮助。

After many try i have successfully send user custom location to other chat user. 经过多次尝试,我已成功将用户自定义位置发送给其他聊天用户。

Used extensions : xep-0080 使用的扩展名: xep-0080

Below i have mention function for send Location 下面我提到发送位置的功能

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)
}

From this function you can also send location to Android(SMACK Lib) 通过此功能,您还可以将位置发送到Android(SMACK Lib)

For didReceiveMessage delegate method you can check attribute. 对于didReceiveMessage委托方法,您可以检查属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM