iOS development uses xmppframework for instant communication (III)

Source: Internet
Author: User

You see today is (c) Yes, there must be (a) and (ii), after the publication of iOS development using Xmppframework for Instant messaging (i) and iOS development using Xmppframework for instant communication (ii) After a lot of small partners add me Q or comment message to ask some questions, such as: "Landlord, where to register?" "," landlord, what is your login username and password? "Sort of thing. In the previous blog used in the account and password, for convenience, is registered with the Spark client, in today's blog will be detailed on how to use the code to register our new users, but also in this thanks to my small friends, thank you for your support!

Okay, nonsense, say to register a new user is actually quite simple, before the use of Xmppframework is directly connected after the direct authentication. The general idea of today's blog is to let the user enter a user name and password, we take the user name to connect our OpenFire server (even if the user is not registered, the Jid can connect to the server). After the connection, the difference is to register the password instead of certification, after the successful registration, and then re-certification.

One, run the show

Or that sentence, in order to be intuitive, is not necessary diagram, the part of the operation is as follows:

Two. Code display

1. In order to prompt, I put alterview in a simple package, easy to use, the code is as follows:

1 // Prompt Box 2 -(void) Alterwithtip: (NSString *) Tip3{4     Uialertview * alter = [[Uialertview alloc] Initwithtitle:@ " hint "delegate: Nil Cancelbuttontitle:@ "OK"  Otherbuttontitles:nil]; 5     [Alter show]; 6 }

2. Start today's theme and get Xmppsteam in our controller first. and register a delegate callback

1- (void) Viewdidload {2 [Super Viewdidload];3 4UIApplication *application =[UIApplication sharedapplication];5     ID Delegate= [ApplicationDelegate];6_xmppstream = [DelegateXmppstream];7     //registering callbacks in the main thread8 [Self.xmppstream adddelegate:self delegatequeue:dispatch_get_main_queue ()];9}

3. Click the Register button, then the connection method, the user name used in the connection method is the user input

1 //Create a connection with the user name entered by the user2-(void) Xmppconnect3 {4NSString *username =Self.userNameTextField.text;5     6     //Create Jid7Xmppjid *jid = [Xmppjid jidwithuser:username domain:mydomain resource:@"IPhone"];8     9     //add Jid to the streamTen [Self.xmppstream Setmyjid:jid]; One      A     //connecting to a server -Nserror *error =Nil; -[Self.xmppstream connectwithtimeout:Tenerror:&ERROR]; the     if(Error) { -NSLog (@"Connection Error%@", [Error localizeddescription]); -     } -}

4. To implement the method of callback after successful connection, register our user password in this method, the code is as follows:

1 //After the connection is successful, the registered user2-(void) Xmppstreamdidconnect: (Xmppstream *) Sender3 {4     5[Self Alterwithtip:@"openfire Server is connected"];6     7NSString *password =Self.passwordTextField.text;8     9Nserror *error =Nil;Ten[Self.xmppstream Registerwithpassword:password error:&ERROR]; One     if(Error) { ANSLog (@"Registration Error%@", [Error localizeddescription]); -     } -}

5. After successful registration, the callback method, after the successful registration, the authentication password:

1 //after successful registration, authentication user password2-(void) Xmppstreamdidregister: (Xmppstream *) Sender3 {4[Self Alterwithtip:@"User Registration Successful"];5     6     //Certified user name and password after successful registration7Nserror *error =Nil;8[Self.xmppstream AuthenticateWithPassword:self.passwordTextField.text error:&ERROR];9     if(Error) {TenNSLog (@"Authentication Error%@", [Error localizeddescription]); One     } A}

6. Callback to achieve successful authentication

1 // callback after successful authentication 2 -(void) Xmppstreamdidauthenticate: (Xmppstream *) sender3{4     [self Alterwithtip:@ " landing success "]; 5 }

7. Callbacks after a failed authentication

1 // callback for authentication failure 2 -(void) Xmppstream:sender didnotauthenticate: (ddxmlelement *) Error3{  4     [self alterwithtip:@ " user name and password error "]; 5 }

Third, attention issues

If you enter an account that is already registered, you will be prompted to register for failure.

Today's blog first come here, welcome criticism

iOS development uses xmppframework for instant communication (III)

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.