Xmppframework for iOS client (4) registering a new account

Source: Internet
Author: User

 

Too lazy to typeFrom: http://www.cnblogs.com/uvsjoh/archive/2012/09/02/2667348.html

1. In-band Registry
In-band registration means that users who have not activated an account on your server can register a new account through the xmpp protocol. The opposite concept is out-of-band registration. For example
You must register on a specified web page.
If the server allows in-band registration, we can register a new account through a self-developed client. The Protocol relating to in-band registration is a XEP-0077.

2. Use XMPPFramework for in-band registration
XMPPStream. h declares the function for simple in-band registration (user name and password are provided for registration ).
-(BOOL) registerWithPassword :( NSString *) password error :( NSError **) errPtr;
You need to establish a stream connection before registration. Because there is no account, you need to establish an anonymous connection.

NSError * err;
NSString * tjid = [[NSString alloc] initWithFormat: @ "anonymous % @", server. text];
[[Delegate xmppStream] setMyJID: [XMPPJID jidWithString: tjid];
If (! [[Delegate xmppStream] connect: & err])
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "failed to connect to the server"
Message: [err localizedDescription]
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];
[AlertView show];

}

After a connection is established, no user verification is required. You can perform the following operations: If the connection is registered, skip the verification step.

- (void)xmppStreamDidConnect:(XMPPStream *)sender{    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);        isXmppConnected = YES;        NSError *error = nil;    if (isRegister == YES)        return;    if (![[self xmppStream] authenticateWithPassword:password error:&error])    {        DDLogError(@"Error authenticating: %@", error);    }}

XmppStream connect is returned immediately, so the connect return does not indicate that the connection has been established.
[Delegate xmppStream] When isConnected is YES, you can confirm that the connection has been established.

After the connection is established, we can send registration information

NSString * jid = [[NSString alloc] initWithFormat: @ "% @", user. text, server. text];
[[Delegate xmppStream] setMyJID: [XMPPJID jidWithString: jid];
NSError * error = nil;
If (! [[Delegate xmppStream] registerWithPassword: password. text error: & error])
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Account creation failed"
Message: [error localizedDescription]
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];
[AlertView show];
}

RegisterWithPassword: the error method is returned immediately.
You need to use the proxy method to determine whether the registration is successful.
If the registration is successful, it will call:-(void) xmppStreamDidRegister :( XMPPStream *) sender
If the registration fails, the system will call-(void) xmppStream :( XMPPStream *) sender didNotRegister :( NSXMLElement *) error.

-(Void) xmppStreamDidRegister :( XMPPStream *) sender {DDLogVerbose (@ "% @: % @", THIS_FILE, THIS_METHOD); registerSuccess = YES; UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Account Created successfully" message: @ "" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alertView show];}-(void) xmppStream :( XMPPStream *) sender didNotRegister :( NSXMLElement *) error {DDLogVerbose (@ "% @: % @", THIS_FILE, THIS_METHOD ); UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "Account creation failed" message: @ "username conflict" delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alertView show];}

 

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.