ios-senior11-Instant Messaging 2 (login registration screen)

Source: Internet
Author: User

1. Introduction of files

1.1 Steps

1.2 Steps

1.3 Steps

1.1 Layout with Storyboard

Login Interface two TextField, two button, enter the user name and password, click Login, Link server success, and authentication success, click Register, jump to the registration screen, enter the user name, password, click Register, Link server success, certification success, and modal return login interface (when the registered user name and password, re-registration will print an error)

2. Create Loginviewcontroller

Extension method

@interface Loginviewcontroller ()

Input box for user name

@property (Weak, nonatomic) Iboutlet Uitextfield *usernametextfied;

Input box for password

@property (Weak, nonatomic) Iboutlet Uitextfield *passwordtextfield;

@end

#pragma mark-Login

-(Ibaction) Loginaction: (ID) Sender {

[Xmppmanager sharedmanaged] directly creates a management object and then directly calls the exposed sign-out method

[[Xmppmanager sharedmanaged] LoginWithUserName:self.userNameTextFied.text Password:self.passwordTextField.text];

}

3. Create Registerviewcontroller

Extension method

@interface Registviewcontroller () <XMPPStreamDelegate>

Registered user Name

@property (Weak, nonatomic) Iboutlet Uitextfield *usernametextfield;

Password for registration

@property (Weak, nonatomic) Iboutlet Uitextfield *passwordtextfield;

@end

-(void)viewdidload {

[Super Viewdidload];

Do any additional setup after loading the view.

Follow protocol (implement modal)

[[Xmppmanager Sharedmanaged].xmppstream adddelegate: Self delegatequeue:d ispatch_get_main_queue ( )];

}

#pragma mark-How to register a response

-(Ibaction) Registaction: (ID) Sender {

[[Xmppmanager sharedmanaged] RegistWithUserName:self.userNameTextField.text Password:self.passwordTextField.text] ;

}

#pragma mark-xmppstreamdelegate Implementation protocol method

Implement the Protocol in order to fulfill the requirement: dismiss to login interface after successful registration

-(void)xmppstreamdidregister:(Xmppstream *) Sender {

NSLog (@ "registered successfully");

Return to the main interface, modal

[Self dismissviewcontrolleranimated:yes completion:^{

NSLog (@ "modal return");

}];

}

Registration failed

-(void)xmppstream:(Xmppstream *) Sender didnotregister:(ddxmlelement *) Error {

NSLog (@ "Registration failed 1111%@", error);

}

4. Package Channel file

4.1 XMPPManager.h

Declaring a channel's properties

@property (Nonatomic,strong) Xmppstream *xmppstream;

Create a single case

+ (Xmppmanager *) sharedmanaged;

How to sign In

-(void) Loginwithusername: (NSString *) userName Password: (NSString *) password;

How to register

-(void) Registwithusername: (NSString *) userName Password: (NSString *) password;

4.2 XMPPMANAGER.M

Declaring a global single-instance variable

Static Xmppmanager *manager = nil;

Create an enumeration type to hold the linked server type interface

typedef ns_enum (Nsuinteger,connecttoserverpurpose) {

Connecttoserverpurposelogin, //login Interface

Connecttoserverpurposeregist, //Registration Interface

};

@interface Xmppmanager () <XMPPStreamDelegate>

Declares a property that records whether the current state is logged in or registered ***********

Note: Enum type attribute, there is no semantic setting, object does not have *

@property (nonatomic) connecttoserverpurpose connecttoserver;

User name

@property (Nonatomic,strong) NSString *username;

Password

@property (Nonatomic,strong) NSString *password;

Registered user Name

@property (Nonatomic,strong) NSString *registusername;

Registered user Password

@property (Nonatomic,strong) NSString *registpassword;

@end

@implementation Xmppmanager

Initialize a single case method

+ (Xmppmanager *) sharedmanaged {

Create a static variable to determine if a manager object has already been generated, and initialize without a build

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

Manager = [[Xmppmanager alloc]init];

});

Return manager;

}

#pragma mark-Create a channel

To register or log in, you need to create a channel, one on the line

-(instancetype) init {

self = [super init];

if (self) {

Initializing the Channel object

Self.xmppstream = [[Xmppstream alloc] init];

Set Opfire server address

Self.xmppStream.hostName = Khostname;

Set server port number

Self.xmppStream.hostPort = Khostport;

Add Agent

[Self.xmppstream adddelegate:self Delegatequeue:dispatch_get_main_queue ()];

}

return self;

}

How to sign In

-(void) Loginwithusername: (NSString *) userName Password: (NSString *) Password {

Self.username = UserName;

Self.password = password;

Logging Login Status ********************************

Self.connecttoserver = Connecttoserverpurposelogin;

Linked server

[Self linkserver];

}

#pragma mark-How to register *******************************

-(void) Registwithusername: (NSString *) userName Password: (NSString *) Password {

Self.registusername = UserName;

Self.registpassword = password;

Record registration Status *********************************

Self.connecttoserver = connecttoserverpurposeregist;

Linked server

[Self linkserver];

}

#pragma mark-linked server

-(void) Linkserver {

Distinguish between a login or a registered ********************

NSString *string = [[NSString alloc]init];

Switch (self.connecttoserver) {

Case Connecttoserverpurposelogin:

string = [NSString stringwithformat:@ "%@",self.username];

Break

Case Connecttoserverpurposeregist:

string = [NSString stringwithformat:@ "%@",self.registusername];

Break

Default

Break

}

To link to a server, you must have user authentication

Id

Parameter 1: User name parameter 2: Domain name parameter 3: Resource

Xmppjid *jid = [Xmppjid jidwithuser: string domain:kdomin Resource:kresource];

Authenticating through a channel

Self.xmppStream.myJID = Jid;

If the current chat tool is in a linked state or is already linked, you need to break the link at this time [this logic is not unique]

if ([Self.xmppstream isconnected] | | [Self.xmppstream isconnecting]) {

[Self disconnecttoserver];

}

Set link timeout

Nserror *error = nil;

if ([Self.xmppstream connectwithtimeout: Error:&error]) {

if (Error) {

NSLog (@ "Server link timeout");

}

}

}

#pragma mark-Break link

-(void) disconnecttoserver{

The current user is offline

Xmpppresence *presence = [xmpppresence presencewithtype:@ "unavailable"];

Tell the tunnel to the line.

[Self.xmppstream sendelement:presence];

This channel loses the link

[Self.xmppstream disconnect];

}

#pragma mark-xmppstreamdelegate protocol method

#pragma mark-Link timed out

-(void)xmppstreamconnectdidtimeout:(Xmppstream *) Sender {

NSLog (@ "link timed out");

}

#pragma mark-Link succeeded

-(void)xmppstreamdidconnect:(Xmppstream *) Sender {

NSLog (@ "link succeeded");

Login Password Verification

Useless, [Self.xmppstream AuthenticateWithPassword:self.password Error:nil];

Differentiate login and registration password Authentication **********

Switch (self.connecttoserver) {

Case Connecttoserverpurposelogin:

Login Password Authentication *********************

[Self.xmppstream AuthenticateWithPassword:self.password Error:nil];

Break

Case Connecttoserverpurposeregist:

Registration Password Authentication *********************

[Self.xmppstream RegisterWithPassword:self.registPassword Error:nil];

Break

Default

Break

}

}

#pragma mark-Break link

-(void)xmppstreamdiddisconnect:(Xmppstream *) sender Witherror: (Nserror *) Error {

1. Manual disconnection

2. Passive Disconnect

if (Error) {

NSLog (@ "Break link, error =%@", error);

}

}

#pragma mark-Authentication failed

-(void) Xmppstream: (Xmppstream *) sender didnotauthenticate:(ddxmlelement *) Error {

NSLog (@ "Authentication failed");

}

#pragma mark-Certification success

-(void)xmppstreamdidauthenticate:(Xmppstream *) Sender {

Online

Xmpppresence *presence = [xmpppresence presencewithtype:@ "available"];

Tell the channel, it's online.

[Self.xmppstream sendelement:presence];

NSLog (@ "Login successful");

}

5. References to third-party documents Xmppframework (introduced as soon as the entry procedure)

ios-senior11-Instant Messaging 2 (login registration screen)

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.