[IOS XMPP] logon to iOS XMPP

Source: Internet
Author: User
Tags call back

[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];}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.