XMPP development tutorial (III)-connection and authentication

Source: Internet
Author: User

From this chapter, I started to explain how to implement some basic functions in the chat, from easy to difficult, which is also the basic method for programming.

In this chapter, we will study the connection and authentication operations.

First, provide code

1 /////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /2 # pragma mark connect/disconnect 3 /////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //// // 4 5-(void) connect {6 7 if (self. xmppstream = nil) {8 self. xmppstream = [[xmppstream alloc] init]; 9 [self. xmppstream Adddelegate: Self delegatequeue: Batch ()]; 10 11 xmpprosterstorage = [[xmpprostercoredatastorage alloc] init]; 12 // xmpprosterstorage = [[xmpprostercoredatastorage alloc] batch]; 13 14 xmpproster = [[xmpproster alloc] initwithrosterstorage: xmpprosterstorage]; 15 16 xmpproster. autofetchroster = yes; 17 xmpproster. autoacceptknownpresencesubscriptionrequests = yes; 18 [XMP Proster activate: xmppstream]; 19 [xmpproster adddelegate: Self delegatequeue: dispatch_get_main_queue ()]; 20} 21 22 23 if (! [Self. xmppstream isconnected]) {24 25 nsstring * [email protected] "[email protected]"; 26 nsstring * [email protected] "***********"; 27 nsstring * [email protected] "helloworldtekimac-mini.local "; 28 29 // set user 30 [xmppstream setmyjid: [xmppjid jidwithstring: userid]; 31 // set server 32 [xmppstream sethostname: Server]; 33 // password 34 Password = pass; 35 36 nserror * error = nil; 37 nstimeinterval timeinter = 15; 38 If (! [Self. xmppstream connectwithtimeout: timeinter error: & error]) {39 nslog (@ "Connect error: % @", [[error userinfo] description]); 40} 41} 42}

Line 8 and 9 initialize the xmppstream object and set delegate to ensure that the corresponding function can be called back later.

14th ~ The 19 rows initialize the xmpproster object and set the storage mode to automatically retrieve data and delegate, which is associated with the xmppstream on the front.

25th ~ 39 rows are connected to the server

Userid is the login user name, pass is the password, server is the server Domain Name
Connectwithtimeout is the latest connection method. Previously, connect was used, but it has been discarded.

If everything goes well, the corresponding method will be called back.
1 - (void)xmppStreamDidConnect:(XMPPStream *)sender {2    // NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];3     NSError *error = nil;4     if (![self.xmppStream authenticateWithPassword:password error:&error]) {5         NSLog(@"Authenticate Error: %@", [[error userInfo] description]);6     }7 }

Row 4th indicates that the password will be verified after the callback is successful, and the following method will be entered:

 1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark Authenticate 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4  5 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { 6     [self goOnline]; 7 } 8  9 -(void)goOnline10 {11     XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];12     [self.xmppStream sendElement:presence];13 }14 15 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error16 {17     NSLog(@"Authenticate Error: %@", error);18 }
-(Void) The goonline method is mainly to send a publish notification to the server.

We run the program, set the breakpoint, and track the path. We can determine whether the user is online based on the server page.



This indicates that both logon and verification are successful. The following section describes how to obtain a friend list.
 

XMPP development tutorial (III)-connection and authentication

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.