XMPP is connected to the server and authentication succeeds

Source: Internet
Author: User

* XMPP features, all requests are implemented by proxy
*
* Because XMPP is data traffic through a Web server, so all the requests are submitted to the server for processing
*
* Server processing is finished, and the agent tells the client to process the results
*
* Official recommendation Appdelegate handle all proxy responses from XMPP servers

AppDelegate.h

////AppDelegate.h//XMPP Practice////Created by Tqh on 15/4/12.//Copyright (c) 2015 TQH. All rights reserved.//#import<UIKit/UIKit.h>#import "XMPPFramework.h"@interfaceAppdelegate:uiresponder <UIApplicationDelegate>@property (Strong, nonatomic) UIWindow*window;#pragmaMark-xmpp related properties and method definitions/** * Global xmppstream, read-only properties*/@property (strong,nonatomic,ReadOnly) Xmppstream *Xmppstream;@end

Appdelegate.m

////APPDELEGATE.M//XMPP Practice////Created by Tqh on 15/4/12.//Copyright (c) 2015 TQH. All rights reserved.//#import "AppDelegate.h"//Note that this does not comply with the Xmppstreamdlegate protocol, and the program will still function properly@interfaceAppdelegate () <XMPPStreamDelegate>/** * Set Xmppstream*/- (void) Setupstream;/** * Notify server users to go online*/- (void) Gooneline;/** * Notify server users offline*/- (void) Gooffline;/** * Link server*/- (void) connect;/** * Disconnected from server*/- (void) DisConnect;@end@implementationappdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//[self connect];    returnYES;}- (void) Applicationwillresignactive: (UIApplication *) application {[Self disConnect];}- (void) Applicationdidbecomeactive: (UIApplication *) application {[Self connect];}#pragmaMark-xmpp Related methods#pragmaMark-Set xmppstream-(void) Setupstream {//avoid being instantiated repeatedly    if(_xmppstream = =Nil) {        //1. Instantiating Xmppstream_xmppstream =[[Xmppstream alloc]init]; //2. Add an agent//because all network requests are done based on network data processing and are not related to the UI interface, you can have proxy methods run in other threads//so as to improve the running performance of the program[_xmppstream adddelegate:self Delegatequeue:dispatch_get_global_queue (Dispatch_queue_priority_default,0)]; }    }#pragmaMark-Notifies the server that the user is online-(void) Gooneline {//1. Instantiate a "show", on-line reportXmpppresence *presence =[xmpppresence presence]; //2. Send presence to the server//when the server knows that "I" is online, I just need to notify my friends without notifying me, so there is no callback for the secondary method .[_xmppstream sendelement:presence]; }#pragmaMark-Notifies the server user of the Downline-(void) gooffline {NSLog (@"User Downline"); //1. Instantiate a "show", Downline reportXmpppresence *presence = [xmpppresence presencewithtype:@"unavailable"]; //2. Send presence to server, notify Server client offline[_xmppstream sendelement:presence];}#pragmaMark-Connect-(void) Connect {//1. Set Xmppstream[self setupstream]; //2. Set user name, password, host (server), do not need password when connectingNSString *username =@"tqhtest"; NSString*hostname =@"127.0.0.1"; //3. Setting the Xmppstream Jid and host//[_xmppstream Setmyjid:[xmppjid jidwithstring:username];//@127.0.0.1[_xmppstream setmyjid:[xmppjid jidwithuser:username domain:@"127.0.0.1"Resource:nil]];    [_xmppstream Sethostname:hostname]; //4. Start the linkNserror *error =Nil; [_xmppstream connectwithtimeout:Tenerror:&ERROR]; //Tip: If you don't make Jid and hostname, you'll get an error and nothing else will go wrong.    if(Error) {NSLog (@"connection request Send error:%@", error.localizeddescription); }Else{NSLog (@"connection request sent successfully"); }}#pragmaMark-Disconnect-(void) DisConnect {//1. Notify Server offline[self gooffline]; //2.XMPPStream disconnecting[_xmppstream disconnect];}#pragmaMark-Proxy method#pragmaMark-The connection is complete (this method is not called if the server address is incorrect)-(void) Xmppstreamdidconnect: (Xmppstream *) Sender {NSLog (@"Connection Creation"); //start sending an authentication requestNserror *error =Nil; NSString*password =@"123456"; [_xmppstream Authenticatewithpassword:password Error:&ERROR]; }#pragmaMark-Authentication Passed-(void) Xmppstreamdidauthenticate: (Xmppstream *) Sender {NSLog (@"Authentication through");}#pragmaMark-Bad password, authentication failed-(void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (Ddxmlelement *) Error {NSLog (@"Authentication failed%@", error); }//<failure xmlns= "URN:IETF:PARAMS:XML:NS:XMPP-SASL" ><not-authorized></not-authorized></ Failure>@end

XMPP is connected to the server and authentication succeeds

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.