XMPP iOS Client implementation three: login, register

Source: Internet
Author: User

1. Create a singleton mode to manage the connection and operation of XMPP

1+ (Xmppmanager *) Share2 {3     StaticXmppmanager *_share=Nil;4     Staticdispatch_once_t Oncetoken;5Dispatch_once (&oncetoken, ^{6_share=[[Xmppmanager alloc] init];7     });8     return_share;9 }Ten-(ID) Init One { A     if(self=[Super Init]) { - [self setupstream]; -     } the     returnSelf ; - } -- (void) Setupstream - { +Xmppstream =[[Xmppstream alloc] init]; - [Xmppstream adddelegate:self delegatequeue:dispatch_get_main_queue ()]; +[Xmppstream SetHostName:@"127.0.0.1"]; A[Xmppstream Sethostport:5222]; at}

2. Login and registration are required to connect to the server using the user name.

-(void) login{[self connect];}-(void) reg{Isreg=YES; [Self connect];}-(BOOL) connect{if(xmppstream.isconnected) {[Self disconnect]; }    if(![Xmppstream isdisconnected]) {        returnYES; }    if(Self.username = =Nil) {        returnNO;    } [Xmppstream Setmyjid:[xmppjid jidWithString:self.userName]; Nserror*error =Nil; if(! [Xmppstream Connectwithtimeout:xmppstreamtimeoutnone error:&ERROR]) {Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Error connecting"message:@"See Console for error details."                                                           Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil];        [Alertview show]; returnNO; }        returnYES;}

3. In the connection agent to determine whether to log in or register, send different commands

//successfully connected server- (void) Xmppstream: (Xmppstream *) sender Socketdidconnect: (Gcdasyncsocket *) socket{Ddlogverbose (@"%@: %@", This_file, This_method);}//failed to connect to server- (void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{Ddlogverbose (@"%@: %@", This_file, This_method);}- (void) Xmppstreamdidconnect: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method); Nserror*error =Nil; if(isreg) {if(! [Xmppstream registerWithPassword:self.pwd error:&ERROR]) {        }    }    Else    {        if(! [Xmppstream authenticateWithPassword:self.pwd error:&ERROR]) {                    }    }    }

4. Handling Login or registration agent functions

//Login Successful- (void) Xmppstreamdidauthenticate: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method);    [Self goonline]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Login Successful"message:@"Login Successful"                                                       Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show];}//Logon Failure- (void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (Nsxmlelement *) error{Ddlogverbose (@"%@: %@", This_file, This_method); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Password Error"message:@"Password Error"                                                       Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil];   [Alertview show]; }//Registration Successful- (void) Xmppstreamdidregister: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Registration Successful"message:@"Registration Successful"                                                       Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil];    [Alertview show]; Isreg=NO;}//Registration Failed- (void) Xmppstream: (Xmppstream *) sender Didnotregister: (Nsxmlelement *) error{Ddlogverbose (@"%@: %@", This_file, This_method); NSLog (@"%@", [[Error Elementforname:@"Error"] stringvalue]); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Registration Failed"message:@"Registration Failed"                                                       Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil];     [Alertview show]; Isreg=NO;}

5. Login or register the username must be "[email protected] hostname" This format

Reprint Please specify: http://www.cnblogs.com/bandy/p/3756492.html

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.