[IOS XMPP] logon to iOS XMPP
1. We recommend setting up an instant chat server. You can use Baidu to set up the server. There are many detailed tutorials.
Openfire is easy to use and written in Java,
Ejabberd is a well-known open-source Erlang project written in Erlang,
2. start logging on
1. Create an XMPPStream object and add a delegate
Add delegate method-(void) addDelegate :( id) delegate delegateQueue :( dispatch_queue_t) delegateQueue
The delegateQueue parameter is the GCD queue used by the delegate callback, and dispatch_get_main_queue () gets the GCD queue of the main thread.
2. Set the JID and Host Name
JID is generally composed of three parts: User Name, domain name and Resource Name, such as test@example.com/Anthony
If no host name is set, the JID domain name is used as the host name.
The port number is optional. The default value is 5222.
3. Connection
- (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]); } }}
Identity Authentication
Implementation-(Void) xmppStreamDidConnect :( XMPPStream *) senderDelegate Method
Call back this method after the server connection is successful.
This method is called after the XML stream has been fully opened. More precisely, this method is called after an opening And Tag have been sent and stored ed, and after the stream features have been encoded ed, and any required features have been fullfilled. At this point it's safe to begin communication with the server.
Identity Authentication Method-(BOOL) authenticateWithPassword :( NSString *) inPassword error :( NSError **) errPtr
- (void)xmppStreamDidConnect:(XMPPStream *)sender { NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@password]; NSError *error = nil; if (![self.xmppStream authenticateWithPassword:password error:&error]) { NSLog(@Authenticate Error: %@, [[error userInfo] description]); }}
Launch
Implementation-(Void) xmppStreamDidAuthenticate :( XMPPStream *) senderDelegate Method
Call back this method after authentication is successful.
This method is called after authentication has successfully finished.
If authentication fails for some reason, the xmppStream: didNotAuthenticate: method will be called instead.
Create an XMPPPresence object. The type is available. Send!
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { XMPPPresence *presence = [XMPPPresence presenceWithType:@available]; [self.xmppStream sendElement:presence];}
Exit and disconnect
Create an XMPPPresence object in the unavailable type. Send!
Disconnect
- (void)disconnect { XMPPPresence *presence = [XMPPPresence presenceWithType:@unavailable]; [self.xmppStream sendElement:presence]; [self.xmppStream disconnect];}