Xmpp is connected to the server and authenticated successfully. xmpp is used for authentication.
* XMPP features that all requests are implemented through proxy
*
* Because xmpp communicates data through the network server, all requests are submitted to the server for processing.
*
* After the server is processed, it stops vomiting and notifies the client of the processing result as a proxy.
*
* AppDelegate is officially recommended to process all proxy responses from XMPP servers.
AppDelegate. h
//// AppDelegate. h // xmpp exercise //// Created by tqh on 15/4/12. // Copyright (c) 2015 tqh. all rights reserved. // # import <UIKit/UIKit. h> # import "XMPPFramework. h "@ interface AppDelegate: UIResponder <UIApplicationDelegate> @ property (strong, nonatomic) UIWindow * window; # attributes and method definitions related to pragma mark-XMPP/*** global xmppstream, read-only attribute */@ property (strong, nonatomic, readonly) XMPPStream * xmppStream; @ end
AppDelegate. m
//// AppDelegate. m // xmpp exercise //// Created by tqh on 15/4/12. // Copyright (c) 2015 tqh. all rights reserved. // # import "AppDelegate. h "// prompt, XMPPStreamDlegate protocol is not followed here, the program can still run normally @ interface AppDelegate () <XMPPStreamDelegate>/*** set xmppStream */-(void) setupStream; /*** notify the server user to go online */-(void) goOneline;/*** notify the server user to go offline */-(void) goOffline; /*** connect to the server */-(void) connect;/*** disConnect from the server */-(void) disConnect; @ end @ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// [self connect]; return YES;}-(void) applicationWillResignActive :( UIApplication *) application {[self disConnect];}-(void) applicationDidBecomeActive :( UIApplication *) application {[self connect];} # pragma mark-XMPP related methods # pragma mark-set XMPPStream-(void) setupStream {// avoid repeated instantiation if (_ xmppStream = nil) {// 1. instantiate XMPPStream _ xmppStream = [[XMPPStream alloc] init]; // 2. add a proxy // because all network requests are processed based on the network and have nothing to do with the UI, therefore, the proxy method can be run in other threads // to improve the running performance of the program [_ xmppStream addDelegate: self delegateQueue: dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];} # pragma mark-notifies the server user of going online-(void) goOneline {// 1. instantiate a "display" and publish the report XMPPPresence * presence = [XMPPPresence presence]; // 2. send presence to the server // After the server knows "I" is online, it only needs to notify my friends without notifying me. Therefore, the second method does not call back [_ xmppStream sendElement: presence] ;}# pragma mark-notifies the server user to go offline-(void) goOffline {NSLog (@ ""); // 1. instantiate a "display" and deprecate the report XMPPPresence * presence = [XMPPPresence presenceWithType: @ "unavailable"]; // 2. send Presence to the server to notify the Server Client to deprecate [_ xmppStream sendElement: presence] ;}# pragma mark-connection-(void) connect {// 1. set XMPPStream [self setupStream]; // 2. set the user name, password, host (server), and password NSString * userName = @ "tqhTest"; NSString * hostName = @ "127.0.0.1"; // 3. set JID and host of XMPPStream // [_ xmppStream setMyJID: [using jidWithString: userName]; // @ 127.0.0.1 [_ xmppStream setMyJID: [XMPPJID jidWithUser: userName domain: @ "127.0.0.1" resource: nil]; [_ xmppStream setHostName: hostName]; // 4. start NSError * error = nil; [_ xmppStream connectWithTimeout: 10 error: & error]; // prompt: If no JID or hostName is specified, an error occurs, if (error) {NSLog (@ "connection request sending error: % @", error. localizedDescription);} else {NSLog (@ "connection request sent successfully") ;}# pragma mark-disConnect-(void) disConnect {// 1. notify the server to go offline [self goOffline]; // 2. XMPPStream disconnection [_ xmppStream disconnect];} # pragma mark-proxy method # pragma mark-Connection completed (if the server address is incorrect, this method will not be called)-(void) xmppStreamDidConnect :( XMPPStream *) sender {NSLog (@ "Connection established"); // start sending the authentication request NSError * error = nil; NSString * password = @ "123456 "; [_ xmppStream authenticateWithPassword: password error: & error] ;}# pragma mark-pass authentication-(void) xmppStreamDidAuthenticate :( XMPPStream *) sender {NSLog (@ "authentication passed") ;}# pragma mark-Incorrect 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> </failure> @ end