如何使用XMPPFramework for iPhone确定用户是否在线/离线?
我有他们的JID等等。有没有办法要求出席或某事?
谢谢。
答案 0 :(得分:7)
您是否查看过XMPPFramework示例项目的源代码?
如果我没记错的话,这应该是相关的代码段:
// Subscribe to the buddy's presence
//
// <presence to="bareJID" type="subscribe"/>
NSXMLElement *presence = [NSXMLElement elementWithName:@"presence"];
[presence addAttributeWithName:@"to" stringValue:[jid bare]];
[presence addAttributeWithName:@"type" stringValue:@"subscribe"];
[xmppStream sendElement:presence];
你的流代表获得的回调应该是
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence;
我假设您已经拥有xmmpframework源,如果没有,您可以在此克隆存储库
hg clone https://xmppframework.googlecode.com/hg/ xmppframework
示例项目位于“Xcode”文件夹中。
答案 1 :(得分:0)
首先,获取用户状态需要在ROSTER List中的双方订阅。
此处,如何订阅和接受在线请求。
let senderName = strReceiverEjabberedName + "@" + eJabVirtualHost
let roomJID = XMPPJID(string: senderName)
// Send subscribe request
let detxTag:XMLElement = XMLElement(name: "presence")
detxTag.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
detxTag.addAttribute(withName: "type", stringValue: "subscribe")
xmppController.xmppStream.send(detxTag)
// Send subscribed request
let detxTaged:XMLElement = XMLElement(name: "presence")
detxTaged.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
detxTaged.addAttribute(withName: "type", stringValue: "subscribed")
xmppController.xmppStream.send(detxTaged)
// Subscribe & Accept Presence Request
xmppController.xmppRoster?.subscribePresence(toUser: roomJID!)
xmppController.xmppRoster?.acceptPresenceSubscriptionRequest(from: roomJID!, andAddToRoster: true)
如果您的朋友向您发送请求,那么您会在名册代表中收到请求。
extension XMPPServiceController: XMPPRosterDelegate {
func xmppRoster(_ sender: XMPPRoster, didReceivePresenceSubscriptionRequest presence: XMPPPresence) {
sender.acceptPresenceSubscriptionRequest(from: presence.from!, andAddToRoster: true)
}
}
完成上述步骤后,您将在下面的 XMPP 委托方法中收到您朋友的在线/离线状态。
func xmppStream(sender: XMPPStream?, didReceivePresence presence: XMPPPresence?) {
let presenceType = presence?.type
if presenceType == "available" { //ONLINE }
else if presenceType == "unavailable" { //OFFLINE }
}