【问题标题】:Empty Roster XMPPFramework Swift空名册 XMPPFramework Swift
【发布时间】:2020-11-11 21:49:01
【问题描述】:

我正在使用 ejabberd 和 Swift 为 iOS 构建一个聊天客户端。当我尝试为用户检索花名册时,尽管该用户有很多好友,但它返回一个空集。我到底哪里错了?我发现了类似的问题,但它们似乎过时了。

这是我的代码:

类 XMPPController: NSObject {

var hostName: String
var hostPort: UInt16
var password: String
var userJID: XMPPJID
var xmppStream: XMPPStream
var xmppRoster: XMPPRoster!
var xmppRosterStorage: XMPPRosterCoreDataStorage!

init(inputHostName: String, inputUserJIDString: String, inputHostPort: UInt16, inputPassword: String) throws {
            
    
    guard let formattedUserJID = XMPPJID(string: inputUserJIDString) else {
        throw XMPPControllerError.wrongUserJID
    }
    
  

    
    self.hostName = inputHostName
    self.hostPort = inputHostPort
    self.password = inputPassword
    self.userJID = formattedUserJID
            
    self.xmppStream = XMPPStream()
    self.xmppStream.hostName = hostName
    self.xmppStream.hostPort = hostPort
    self.xmppStream.myJID = userJID
    self.xmppRosterStorage = XMPPRosterCoreDataStorage()
    self.xmppRoster = XMPPRoster(rosterStorage: xmppRosterStorage)

    

    super.init()
  
    xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)
    xmppRoster.addDelegate(self, delegateQueue: DispatchQueue.main)

    xmppRoster.autoFetchRoster = true;
    xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = true;
    xmppRoster.activate(xmppStream)

    print(xmppRosterStorage.jids(for: xmppStream))
    

}


func connect() {
    
    
    if self.xmppStream.isDisconnected {
        
    }
    
    do {
        try self.xmppStream.connect(withTimeout: 5)
        
        
    } catch {
        print("Error Connecting")
    }
    
}

func disconnect(){
    
    self.xmppStream.disconnect()
    
}

}

扩展 XMPPController: XMPPStreamDelegate {

func xmppStreamDidConnect(_ sender: XMPPStream) {
    print("Stream: Connected")
    try! sender.authenticate(withPassword: password)
    
    
}

func xmppStreamDidDisconnect(_ sender: XMPPStream, withError error: Error?) {
    print("Stream: Disconnected")
}

func xmppStreamDidAuthenticate(_ sender: XMPPStream) {
    let presence = XMPPPresence()
    self.xmppStream.send(presence)
    
    

    print("Stream: Authenticated")
    
    NotificationCenter.default.post(name: Notification.Name(rawValue: authenticatedNotificationKey), object: self)
    
}

func xmppStream(_ sender: XMPPStream, didNotAuthenticate error: DDXMLElement) {
    print("Wrong credentials")
}

}

谢谢。

【问题讨论】:

    标签: swift xmpp xmppframework


    【解决方案1】:

    回答我自己的问题。

    两个问题。

    1. 我没有为 XMPPController 定义超类 XMPPRosterDelegate。

    2. 我没有打电话

      func xmppRosterDidEndPopulating(_ sender: XMPPRoster) {

       print(xmppRosterStorage.jids(for: xmppStream))
      

      }

    只有声明 XMPPRosterDelegate 才能完成的事情。

    【讨论】:

      【解决方案2】:

      我看不到您将任何用户添加到您的名册中。你可以试试这个:

      func sendSubscribePresenceFromUserRequest ( _ username : String) {
          let otherUser = XMPPJID(string: "\(username)@\(xmppDomain)")!
          self.roster.addUser(otherUser, withNickname: "Other user name" , groups: nil, subscribeToPresence: true)
      }
      

      然后你在 'xmppStreamDidAuthenticate' 方法之后调用 setup XMPPRoster 方法,就像这样:

      func rosterSetup() {
          let storage = XMPPRosterCoreDataStorage.sharedInstance()
          roster = XMPPRoster(rosterStorage: storage!, dispatchQueue: DispatchQueue.main)
          if let rosterXmpp = roster {
              rosterXmpp.setNickname("Your name" , forUser: stream.myJID!)
              rosterXmpp.activate(stream)
              rosterXmpp.addDelegate(self, delegateQueue: DispatchQueue.main)
              rosterXmpp.autoFetchRoster = true
              rosterXmpp.autoClearAllUsersAndResources = true
              rosterXmpp.autoAcceptKnownPresenceSubscriptionRequests = true
              rosterXmpp.fetch()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-21
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 2017-01-05
        • 1970-01-01
        • 2014-06-12
        相关资源
        最近更新 更多