This paper links: http://www.cocoachina.com/ios/20141219/10703.html

XMPPFramework is an open source project of OS X/iOS platform. It implements THE XMPP protocol (RFC-3920) using Objective-C, and also provides tools for reading and writing XML, greatly simplifying the development of communication applications based on XMPP

1. Login and logout with friends

1.1 Common objects in XMPP

XMPPStream: XMPP base service class

XMPPRoster: Friend list class

XMPPRosterCoreDataStorage: buddy list (user accounts) in the core data operations in the class

XMPPvCardCoreDataStorage: Action class for friends’ business cards (nickname, signature, gender, age, etc.) in CoreData

XMPPvCardTemp: Friend card entity class that is pulled from the database

XmppvCardAvatarModule: Friend avatar

XMPPReconnect: Automatically reconnects if the connection is lost

XMPPRoom: Provides multi-user chat support

XMPPPubSub: Publish subscription

1.2 Logging In to the XMPP server

– (void)connect {

if (self.xmppStream == nil) {

self.xmppStream = [[XMPPStream alloc] init];

[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];

}

if (! [self.xmppStream isConnected]) {

NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@”username”];

XMPPJID *jid = [XMPPJID jidWithUser:username domain:@”lizhen” resource:@”Ework”];

[self.xmppStream setMyJID:jid];

[self. XmppStream setHostName: @ “10.4.125.113”].

NSError *error = nil;

if (! [self.xmppStream connect:&error]) {

NSLog(@”Connect Error: %@”, [[error userInfo] description]);

}

}

}

After connect succeeds, the XMPPStreamDelegate method is called in turn, first

– (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket

– (void)xmppStreamDidConnect:(XMPPStream *)sender

Under this method you need to use xmppStream’s authenticateWithPassword method for password authentication, which responds to delegate’s method, which is the one below

– (void)xmppStreamDidAuthenticate:(XMPPStream *)sender


Patch 1.3

Implementation – (void) xmppStreamDidAuthenticate: (XMPPStream *) sender delegate

– (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {

XMPPPresence *presence = [XMPPPresence presenceWithType:@”available”];

[self.xmppStream sendElement:presence];

}

1.4 Exit and disconnect the connection

– (void)disconnect {

XMPPPresence *presence = [XMPPPresence presenceWithType:@”unavailable”];

[self.xmppStream sendElement:presence];

[self.xmppStream disconnect];

}

1.5 Friend Status

Get the status of a friend by implementing

– (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence

.

Presence status:

The available online

Leave away

Don’t disturb your neighbor

Unavailable offline

2. Receive and send messages

2.1 Receiving Messages

By implementing

– (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;

methods

When the contents of the Message tag are received, the XMPPFramework framework calls back to this method

According to the XMPP protocol, the contents of the message body are stored in the tag body

– (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {

NSString *messageBody = [[message elementForName:@”body”] stringValue];

}

2.2 Sending Messages

To send a message, we need to put the data in the tag according to the XMPP protocol,





– (void)sendMessage:(NSString *) message toUser:(NSString *) user {

NSXMLElement *body = [NSXMLElement elementWithName:@”body”];

[body setStringValue:message];

NSXMLElement *message = [NSXMLElement elementWithName:@”message”];

[message addAttributeWithName:@”type” stringValue:@”chat”];

NSString *to = [NSString stringWithFormat:@”%@@example.com”, user];

[message addAttributeWithName:@”to” stringValue:to];

[message addChild:body];

[self.xmppStream sendElement:message];

}

3. Get friend information and delete friends

3.1 Friend list and friend name card

[_xmppRoster fetchRoster]; // Get the list of friends

// Get a friend node

– (void)xmppRoster:(XMPPRoster *)sender didRecieveRosterItem:(NSXMLElement *)item

// Get the list of friends

– (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender

// Go to the server to request contact name card information

– (void)fetchvCardTempForJID:(XMPPJID *)jid;

// Request the name card of the contact, if the database has no request, send the name card request

– (void)fetchvCardTempForJID:(XMPPJID *)jid ignoreStorage:(BOOL)ignoreStorage;

// Retrieve the name card of the contact, return if there is no return empty, and fetch the server

– (XMPPvCardTemp *)vCardTempForJID:(XMPPJID *)jid shouldFetch:(BOOL)shouldFetch;

// Update your business card information

– (void)updateMyvCardTemp:(XMPPvCardTemp *)vCardTemp;

// Get a callback to a box of contacts’ name cards

– (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule

didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp

forJID:(XMPPJID *)jid