robbiehanson / XMPPFramework

An XMPP Framework in Objective-C for Mac and iOS
Other
5.91k stars 2.09k forks source link

When updating vcard i can't send or recieve messages #1144

Open iorisam opened 5 years ago

iorisam commented 5 years ago

`import UIKit import XMPPFramework

class TestingViewController: UIViewController { var serverIp = "192.168.1.5"

var xmppStream: XMPPStream!
var xmppRosterStorage = XMPPRosterCoreDataStorage()
var xmppRoster: XMPPRoster!
var xmppvCardTempModule : XMPPvCardTempModule!
var xmppvCardStorage: XMPPvCardCoreDataStorage!
var hostName: String = ""
var userJID: XMPPJID!
var hostPort: UInt16 = 5222
var password: String = ""
var callbacks: CallbacksWrapper?

override func viewDidLoad() {
    super.viewDidLoad()
    xmppStream = XMPPStream()
    hostName = serverIp
    hostPort = 5222
    password = "azeqsdwxc"
    userJID = XMPPJID(string: "sam@winsality64")
    userJID = userJID!

    self.xmppStream.hostName = hostName
    self.xmppStream.hostPort = hostPort
    self.xmppStream.startTLSPolicy = XMPPStreamStartTLSPolicy.allowed
    self.xmppStream.myJID = userJID

     self.xmppStream.addDelegate(self, delegateQueue: DispatchQueue.main)

}

func createProfile (){
    let profile = Profile.shared
    profile.id = UUID().uuidString
    let username = "sam"
    profile.jid = ("\(username)@winsality64")
    profile.UserName = username
    profile.image = UIImage(named: "DefaultUser2")
    profile.password = "azeqsdwxc"
    profile.updateProfile()
}

func sendMsg(receiver : String, textmsg : String){
    print("we are trying to send msg to",receiver)
    let user = XMPPJID(string: receiver)
    let msg = XMPPMessage(type: "chat", to: user)
    msg.addBody(textmsg)
    self.xmppStream.send(msg)
}

@IBAction func MeButtonPushed(_ sender: Any) {
    connect()
}

func connect() {
    if self.xmppStream.isConnected {
            return
    }
    try! self.xmppStream.connect(withTimeout: XMPPStreamTimeoutNone)
}

func UpdateVcard(profile : Profile) -> Bool{
    print("inside the update vcard func")
    if (self.xmppStream.isAuthenticated)  {
        self.xmppvCardStorage = XMPPvCardCoreDataStorage.sharedInstance()
        self.xmppvCardTempModule = XMPPvCardTempModule.init(vCardStorage: self.xmppvCardStorage!)
        self.xmppvCardTempModule.activate(self.xmppStream)
        var myVcardTemp = self.xmppvCardTempModule.myvCardTemp
        let vCardXML = DDXMLElement(name: "vCard", stringValue: "vcard-temp")

        if (myVcardTemp == nil) {
            myVcardTemp = XMPPvCardTemp.vCardTemp(from: vCardXML)
            myVcardTemp!.nickname = profile.UserName
            let image = profile.image
            myVcardTemp?.photo = image?.jpegData(compressionQuality: 1.0)
            self.xmppvCardTempModule.updateMyvCardTemp(myVcardTemp!)
            self.xmppvCardTempModule.deactivate()
            print("new vcard updated thank you ")
        }else{
            print("old nickname is  ",myVcardTemp!.nickname)
            myVcardTemp!.nickname = profile.UserName
            let image = profile.image
            myVcardTemp!.photo = image?.jpegData(compressionQuality: 1.0)
            self.xmppvCardTempModule.updateMyvCardTemp(myVcardTemp!)
            self.xmppvCardTempModule.deactivate()
            print("trying to parse vcard here")
            self.sendMsg(receiver: "iori", textmsg: "hello my mate")

        }
        return true
    }
    else {
        return false
    }
}

}

extension TestingViewController: XMPPStreamDelegate {

func xmppStream(_ sender: XMPPStream, didReceive iq: XMPPIQ) -> Bool {
    print("we recieved an iq from xmpp ",iq)
    let friendvcard = XMPPvCardTemp.vCardTempCopy(from: iq)
    if(friendvcard != nil){
        let userInfo = ["jid": iq.from,"nickname":friendvcard?.nickname,"photo": friendvcard?.photo] as [String : Any]
        print("we recieved an iq from xmpp ",friendvcard!.nickname)
    }

    return true

}
func xmppStreamDidConnect(_ stream: XMPPStream) {
    print("Stream: Connected")

    if(!stream.isAuthenticated && !stream.isConnecting){
        try! stream.authenticate(withPassword: self.password)
    }else{

        print("connected but already authenticated")
    }
}

func xmppStreamDidAuthenticate(_ sender: XMPPStream) {

    self.xmppStream.send(XMPPPresence())
    print("Stream: Authenticated")
    let profile = Profile.shared
    self.UpdateVcard(profile: profile)

}

func xmppStream(_ sender: XMPPStream, didNotAuthenticate error: DDXMLElement) {
    print("Stream: Fail to Authenticate")
}

func xmppStreamDidDisconnect(_ sender: XMPPStream, withError error: Error?) {
    print("there was a disconnection i believe sorry ")
}

func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
    print("the message is from",message.from?.bareJID.bare)
    print("the message body is",message.body)
}

func xmppStream(_ sender: XMPPStream, didReceive presence: XMPPPresence) {
    let presenceType = presence.type
    let myUsername = sender.myJID?.user
    let presenceFromUser = presence.from?.user
    let userInfo = ["text": presenceType!,"senderUserName": presenceFromUser]
    print("Did receive presence from \(presenceFromUser)")

}

}`