IOS XMPP (2) Create your own client

Source: Internet
Author: User

I. Purpose and Effect:

Using the XMPP framework to build a client with Xcode, to register to the server to add user passwords, as well as login, offline status

The main structure of the project: New Singleview project, with xib drag and drop two input boxes and two buttons,

and associate in Viewcontroller, establish Regisgtviewcontroller and select Xib, use Xib Association

The final effect can be viewed in the Web OpenFire Admin Console

Two. Specific process:

Key frame: #import "XMPPFramework.h"

Establish a single case Viewcontroller

Singleton section:

The. h file

A single example initialization method

+ (instancetype) Sharemanager;

Method of landing

-(void) Loginwithname: (NSString *) name Andpassword: (NSString *) password block: (void (^) (nsstring *name,nsstring * Password,int Resulttype)) block;

Registration method

-(void) Registerwithname: (NSString *) name Andpassword: (NSString *) password block: (void (^) (nsstring *name,nsstring * Password,int Resulttype)) block;

Cancellation

-(void) logOut;

In the. m file

(self-written) extended private method of the class

@interface XMPPManager1 () <XMPPStreamDelegate>

{

Use block for callback processing

__block void (^_block) (NSString *name,nsstring *password,int resulttype);

}

@property (Nonatomic,strong) Xmppstream *stream;

Log User name password

@property (Nonatomic,strong) NSString *name;

@property (Nonatomic,strong) NSString *password;

set bool Value Login and registration method different need to judge

@property (nonatomic,assign) int type;

@end

@implementation XMPPManager1

A single example initialization method

+ (Instancetype) Sharemanager

{

static XMPPManager1 *share = nil;

Static dispatch_once_t Onectoken;

Dispatch_once (&onectoken, ^{

Share = [[XMPPManager1 alloc]init];

});

return share;

}

Initialize only once

-(ID) init

{

if ([Super init]) {

Initialize Xmppstream

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

Set the address of the server

Self.stream.hostName = @ "127.0.0.1";

Setting up ports

Self.stream.hostPort = 5222;

Set up Proxy

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

}

return self;

}

Method of landing

-(void) Loginwithname: (NSString *) name Andpassword: (NSString *) password block: (void (^) (nsstring *name,nsstring * Password,int Resulttype)) block

{

Self.type = 1;//Login

Self.name = name;//Record user

Self.password = password;//record password

_block = Block;

Set up users

Xmppjid *jid = [Xmppjid jidwithuser:name domain:@ "wuxiaoyuan.local" resource:@ "iphone"];

Self.stream.myJID = Jid;

Calling a connection method

[Self connecttoserver];

}

Extracting a method to connect to a server

-(void) connecttoserver

{

Tap in the connected state again to disconnect

if ([Self.stream isconnected]) {

[Self logOut];

}

Nserror *error = nil;

[Self.stream Connectwithtimeout:xmppstreamtimeoutnone error:&error];

if (Error) {

NSLog (@ "%@", error);

}

}

Registration method

-(void) Registerwithname: (NSString *) name Andpassword: (NSString *) password block: (void (^) (nsstring *name,nsstring * Password,int Resulttype)) block

{

Self.type = 2;//Registration

Self.name = name;//Record user

Self.password = password;//record password

_block = Block;

Set up users

Xmppjid *jid = [Xmppjid jidwithuser:name domain:@ "wuxiaoyuan.local" resource:@ "iphone"];

Self.stream.myJID = Jid;

Calling a connection method

[Self connecttoserver];

}

Cancellation

-(void) LogOut

{

change user status to Offline

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

[Self.stream sendelement:presence];

Disconnect Connection

[Self.stream disconnect];

}

#pragma mark--proxy method

Connection failed

-(void) Xmppstreamconnectdidtimeout: (Xmppstream *) sender

{

NSLog (@ "Connection failed");

if (_block) {

_block (self.name,self.password,-1);

}

Need to disconnect

[Self.stream disconnect];

}

Connection Successful

-(void) Xmppstreamdidconnect: (Xmppstream *) sender

{

if (Self.type = = 1) {

Send password Authentication to the server (verification success and validation failure)

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

}else if (Self.type = = 2)

{

Send a password to the server for registration (verification success and validation failure)

[Self.stream RegisterWithPassword:self.password Error:nil];

}

}

//----------------------

Validation failed

-(void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (ddxmlelement *) error

{

NSLog (@ "Verification failed");

if (_block) {

_block (self.name,self.password,0);

}

Need to disconnect

[Self.stream disconnect];

}

Validation successful

-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender

{

change user status to Online

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

[Self.stream sendelement:presence];

}

//----------------------

//----------------------

Registration failed

-(void) Xmppstream: (Xmppstream *) sender Didnotregister: (ddxmlelement *) error

{

NSLog (@ "registration failed");

if (_block) {

_block (self.name,self.password,0);

}

Need to disconnect

[Self.stream disconnect];

}

Registration successful

-(void) Xmppstreamdidregister: (Xmppstream *) sender

{

NSLog (@ "registered successfully");

if (_block) {

_block (self.name,self.password,1);

}

Need to disconnect

[Self.stream disconnect];

}

//----------------------

Invoking a singleton in another controller

Key statement:

[[XMPPManager1 Sharemanager]registerwithname:self.userfield.text AndPassword:self.passwordField.text block:^ ( NSString *name, nsstring *password, int resulttype) {

if (Resulttype = = 1) {

[Self dismissviewcontrolleranimated:yes completion:nil];

}

}];

IOS XMPP (2) Create your own client

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.