1. Basic steps: First import the XMPP framework, configure the environment
-"Since we are using OC's XMPP framework, we need to bridge the development of Swift." The specific method is to create a C-based. h header file, and then add the header file of the language that we need to compile OC to contain this. h file;
-"Then import the following library files, in the head file selection path
For example, to set the search path for header search paths, import the LIBXML2 library file relative path, and note that the USR is in front. The first time I installed it, I wrote the user for more than half an hour.
And then create the interface, just get two boxes and buttons to customize a suggested interface.
The user name and password in the application should be used repeatedly, so we need to say that the door is set to a simple interest class.
As shown in the following code:
Import UIKit
Class Wsbuserinfo:nsobject {
User name password for login
var username:string?
var userpasswd:string?
Registered User name password
var userregistername:string?
var userregisterpasswd:string?
To differentiate between login and registration
var islogin:bool?
Single case
Class Func getshareinstance (), wsbuserinfo{
struct Singleton {
static var dispatchone:dispatch_once_t = 0
static Var instance:wsbuserinfo? = Nil
}
Dispatch_once (&singleton.dispatchone) {(), Void in
Singleton.instance = Wsbuserinfo ()
}
Return singleton.instance!
}
}
Because the login process is designed to implement the XMPP streaming protocol, there are many steps. Here we can individually encapsulate a tool class. This allows us to log in and register the host controller to get a great relief:
In the login controller we only need to assign the input box user input information to the single-use household information class userinfo.
Then call the Usrlogin () method of the simple interest tool class and automatically complete all the login processes
By specifying a protocol, the login and registration status can be monitored.
The logic of registering the controller is the same as above, it is not repeated here.
Note 1: Before each connection request, the previous connection is disconnected, this can avoid an account long connection and other accounts can not connect.
Note 2: Re-sending login registration involves common connection requests and connection status requests, and we need to establish a bool attribute value in the simple interest class to make a branching decision, so that we only need to make a simple adjustment within the method, thus eliminating a lot of code steps.
Please see the following code for specific steps
Import UIKit
Class Wsbloginviewcontroller:uiviewcontroller,wsbxmpplogindelegate {
@IBOutlet weak var usernamefield:uitextfield!
@IBOutlet weak var userpasswdfield:uitextfield!
@IBAction func Loginbtnclick (sender:anyobject) {
Wsbuserinfo.getshareinstance (). IsLogin = True
Print ("User name: \ (usernamefield.text!)")
Print ("Password is: \ (userpasswdfield.text!)")
The user name password is stored in the tool class Wsbuserinfo this singleton object
Wsbuserinfo.getshareinstance (). UserName = usernamefield.text!
Wsbuserinfo.getshareinstance (). userpasswd = userpasswdfield.text!
Call the XMPP tool class to complete the login
Set up an agent for Wsbxmpptool before logging in
Wsbxmpptool.getsharedinstance (). Logindelegate = Self
Wsbxmpptool.getsharedinstance (). Userlogin ()
}
/** methods in the login protocol */
Func loginsuccess () {
Print ("Login Successful")
Switch to the main interface
Let StoryBoard = Uistoryboard.init (name: "Main", Bundle:nil)
Uiapplication.sharedapplication (). Keywindow?. Rootviewcontroller =
Storyboard.instantiateinitialviewcontroller ()
}
Func loginfailed () {
Print ("Login Failed")
}
Func Loginneterror () {
Print ("Network error at logon")
}
Override Func Viewdidload () {
Super.viewdidload ()
}
deinit{
Print ("deinit\ (self)")
}
}
Registerviewcontroller:
Import UIKit
Class Wsbregisterviewcontroller:uiviewcontroller,wsbxmppregisterdelegate {
@IBOutlet weak var userregisername:uitextfield!
@IBOutlet weak var userregisterpasswd:uitextfield!
@IBAction func Registerbtnclick (sender:anyobject) {
Indicates that this is a registration behavior
Wsbuserinfo.getshareinstance (). IsLogin = False
Wsbuserinfo.getshareinstance (). Userregistername = self.userregisername.text!
Wsbuserinfo.getshareinstance (). userregisterpasswd = self.userregisterpasswd.text!
To invoke the registration method of Xmpptool
Wsbxmpptool.getsharedinstance (). Registerdelegate = Self
Wsbxmpptool.getsharedinstance (). Userregister ()
}
/** implementing the method in the Registration protocol */
Func registersuccess () {
Print ("Registration successful")
Self.dismissviewcontrolleranimated (True, Completion:nil)
}
Func registerfailed () {
Print ("Registration failed")
}
Func Registerneterror () {
Print ("Network error at Registration")
}
}
Login and Registration agreement
Import Foundation
Login protocol
Protocol Wsbxmpplogindelegate:nsobjectprotocol {
Login successful
Func loginsuccess ()
Logon failure
Func loginfailed ()
Network error
Func Loginneterror ()
}
Registration Agreement
Protocol Wsbxmppregisterdelegate:nsobjectprotocol {
Registration successful
Func registersuccess ()
Registration failed
Func registerfailed ()
Registering a network error
Func Registerneterror ()
}
Swift:
Import UIKit
/** Bridging file Item--buildsettings Search objective-c Bride Settings
This is an XMPP tool class that can complete login and register lines
*/
Class Wsbxmpptool:nsobject, Xmppstreamdelegate {
var xmppstream:xmppstream!
Delegate of Login
Weak var logindelegate:wsbxmpplogindelegate?
Registered Deletegate
Weak var registerdelegate:wsbxmppregisterdelegate?
Ensure that Xmppstream is not empty when used
Override Init () {
Super.init ()
Setupxmppstream ()
}
Class Func getsharedinstance (), Wsbxmpptool {
struct Singleton {
static var predicate:dispatch_once_t = 0
static Var instance:wsbxmpptool? = Nil
}
Dispatch_once (&singleton.predicate) {(), Void in
Singleton.instance = Wsbxmpptool ()
}
Return singleton.instance!
}
Set up the Flow settings agent
Func Setupxmppstream () {
Xmppstream = Xmppstream ()
Xmppstream.adddelegate (self, delegatequeue:dispatch_get_main_queue ())
}
Connecting to a server
Func Connecttohost () {
Disconnect from the last log on and you won't be able to log in again
Xmppstream.disconnect ();
Call Settings Xmppstream
if Self.xmppstream = = Nil {
Self.setupxmppstream ()
}
var username:string? = Nil
If Wsbuserinfo.getshareinstance (). islogin! {
UserName = Wsbuserinfo.getshareinstance (). UserName
}else{
UserName = Wsbuserinfo.getshareinstance (). Userregistername
}
Build Jid settings hostname and port
Xmppstream.myjid = Xmppjid.jidwithuser (userName, Domain:xmppdomain, resource: "iphone")
Xmppstream.hostname = Xmpphostname
Xmppstream.hostport = Xmppport
do {
Try Xmppstream.connectwithtimeout (Xmppstreamtimeoutnone)
}catch let error as nserror{
Print (error.description)
}
}
Connection succeeded (proxy method)
Func Xmppstreamdidconnect (sender:xmppstream!) {
Call to send password request authorization
If Wsbuserinfo.getshareinstance (). islogin! {
SELF.SENDLOGINPASSWD ()
}else{
Register with a password
SELF.SENDREGISTERPASSWD ()
}
}
Send Password Registration
Func sendregisterpasswd () {
do{
Try Xmppstream.registerwithpassword (Wsbuserinfo.getshareinstance (). userregisterpasswd)
}catch let error as nserror{
Print (error.description)
}
}
Send Password Request authorization
Func sendloginpasswd () {
bo=
Try Xmppstream.authenticatewithpassword (Wsbuserinfo.getshareinstance (). userpasswd)
}catch let error as nserror{
Print (error.description)
}
}
Authorization Success (proxy method)
Func xmppstreamdidauthenticate (sender:xmppstream!) {
Print ("Authorization succeeded")
Send online message
Self.sendonline ()
Method of invoking the proxy's login success
Logindelegate?. Loginsuccess ()
}
Send online message
Func Sendonline () {
Xmppstream.sendelement (Xmpppresence ())
}
Authorization failure
Func Xmppstream (sender:xmppstream!, Didnotauthenticate error:ddxmlelement!) {
Print (error.description)
Authorization failed to invoke the agent's logon failure method
Logindelegate?. LoginFailed ()
}
Connection disconnect (proxy method)
Func xmppstreamdiddisconnect (sender:xmppstream!, Witherror error:nserror!) {
If the network has a problem disconnecting, there is a value in the error
The error is nil if you are actively disconnecting
If error! = Nil {
If Wsbuserinfo.getshareinstance (). islogin! {
Network error calling agent for logon network problem
Logindelegate?. Loginneterror ()
}else{
Network issues during registration
Registerdelegate?. Registerneterror ()
}
}
}
Registration Successful (proxy method)
Func Xmppstreamdidregister (sender:xmppstream!) {
Successful registration of the calling agent
Registerdelegate?. Registersuccess ()
}
Registration failed (proxy method)
Func Xmppstream (sender:xmppstream!, Didnotregister error:ddxmlelement!) {
Registerdelegate?. Registerfailed ()
}
How the user completes the login
Func Userlogin () {
Self.connecttohost ()
}
How the user completes the registration
Func Userregister () {
Self.connecttohost ()
}
}
6. Swift enables a simple login registration based on XMPP and OpenFire