Skip to content

Commit

Permalink
Swift XMPP 客户端
Browse files Browse the repository at this point in the history
Swift XMPP 客户端,Xcode 6.1 beta上测试. by 小波.
QQ:41359833  本代码视频教程: xiaoboswift.com (小波说雨燕 第四季)
  • Loading branch information
bo2870000 authored and bo2870000 committed Sep 28, 2014
1 parent e7a427d commit b272420
Show file tree
Hide file tree
Showing 106 changed files with 37,473 additions and 39 deletions.
538 changes: 534 additions & 4 deletions weixin.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,63 @@
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE0C19CFE8570024AFF8"
BuildableName = "weixin.app"
BlueprintName = "weixin"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE2119CFE8570024AFF8"
BuildableName = "weixinTests.xctest"
BlueprintName = "weixinTests"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE2119CFE8570024AFF8"
BuildableName = "weixinTests.xctest"
BlueprintName = "weixinTests"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE0C19CFE8570024AFF8"
BuildableName = "weixin.app"
BlueprintName = "weixin"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand All @@ -23,6 +72,15 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE0C19CFE8570024AFF8"
BuildableName = "weixin.app"
BlueprintName = "weixin"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
Expand All @@ -32,6 +90,15 @@
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "10DFDE0C19CFE8570024AFF8"
BuildableName = "weixin.app"
BlueprintName = "weixin"
ReferencedContainer = "container:weixin.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
Expand Down
170 changes: 169 additions & 1 deletion weixin/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,177 @@
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class AppDelegate: UIResponder, UIApplicationDelegate, XMPPStreamDelegate {

var window: UIWindow?

//通道
var xs : XMPPStream?
//服务器是否开启
var isOpen = false
//密码
var pwd = ""

//状态代理
var ztdl: ZtDL?

//消息代理
var xxdl: XxDL?

//收到消息
func xmppStream(sender: XMPPStream!, didReceiveMessage message: XMPPMessage!) {

// 如果是聊天消息
if message.isChatMessage() {
var msg = WXMessage()

//对方正在输入
if message.elementForName("composing") != nil {
msg.isComposing = true
}

//离线消息
if message.elementForName("delay") != nil {
msg.isDelay = true
}

//消息正文
if let body = message.elementForName("body") {
msg.body = body.stringValue()
}

//完整用户名
msg.from = message.from().user + "@" + message.from().domain

//添加到消息代理中
xxdl?.newMsg(msg)

}
}

//收到状态
func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
//我自己的用户名
let myUser = sender.myJID.user

//好友的用户名
let user = presence.from().user

//用户所在的域
let domain = presence.from().domain

//状态类型
let pType = presence.type()

//如果状态不是自己的
if (user != myUser) {
//状态保存的结构
var zt = Zhuangtai()

//保存了状态的完整用户名
zt.name = user + "@" + domain

//上线
if pType == "available" {
zt.isOnline = true
ztdl?.isOn(zt)

} else if pType == "unavailable" {
ztdl?.isOff(zt)

}

}


}




//连接成功
func xmppStreamDidConnect(sender: XMPPStream!) {
isOpen = true
//验证密码
xs!.authenticateWithPassword(pwd, error: nil)
}

//验证成功
func xmppStreamDidAuthenticate(sender: XMPPStream!) {
//上线
goOnline()
}




//建立通道
func buildStream() {
xs = XMPPStream()
xs?.addDelegate(self, delegateQueue: dispatch_get_main_queue())
}

//发送上线状态
func goOnline() {
var p = XMPPPresence()

xs!.sendElement(p)
}

//发送下线状态
func goOffline() {
var p = XMPPPresence(type: "unavailabe")

xs!.sendElement(p)
}

//连接服务器(查看服务器是否可连接)
func connect() -> Bool {
//建立通道
buildStream()

//通道已经连接
if xs!.isConnected() {
return true
}

//取系统中保存的用户名/密码/服务器地址
let user = NSUserDefaults.standardUserDefaults().stringForKey("weixinID")
let password = NSUserDefaults.standardUserDefaults().stringForKey("weixinPwd")
let server = NSUserDefaults.standardUserDefaults().stringForKey("wxserver")

if (user != nil && password != nil) {

//通道的用户名
xs!.myJID = XMPPJID.jidWithString(user!)

xs!.hostName = server!

//密码保存备用
pwd = password!

xs!.connectWithTimeout(5000, error: nil)

}



return false
}


//断开连接
func disConnect() {
if xs != nil {
if xs!.isConnected() {
goOffline()
xs!.disconnect()
}
}

}




func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Expand Down
8 changes: 4 additions & 4 deletions weixin/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6246" systemVersion="14A361c" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6239"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<objects>
Expand All @@ -11,13 +11,13 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2014年 q. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="xiaoboswift.com" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="441" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="weixin" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Swift小微信" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="441" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
Expand Down
Loading

0 comments on commit b272420

Please sign in to comment.