Skip to content

zhangxigithub/Bonjour

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bonjour

communicate with other iOS devices nearby

简单封装使用MultipeerConnectivity,是附近的设备可以通过wifi或者蓝牙传递消息(字符串)

img

视频demo

###首先 引入Bonjour.swift文件

###使用

//启动bonjour
Bonjour.sharedBonjour.bonjour() //默认用设备名 UIDevice.currentDevice().name
//或者
Bonjour.sharedBonjour.bonjour("自定义名称")

###发送消息

Bonjour.sharedBonjour.sendMessage("message")

###接受消息和状态有两种方法,使用其中一种即可。

#####使用代理(接受消息、获取状态)

//实现BonjourDelegate
Bonjour.sharedBonjour.delegate = self

func didReceiveMessage(message: String,peerID:MCPeerID) {
//接收到消息
}
func didConnectPeer(peerID: MCPeerID) {
//连接到
}
func didDisconnectPeer(peerID: MCPeerID) {
//失去连接
}

#####使用消息(接受消息、获取状态)

NSNotificationCenter.defaultCenter().addObserverForName(BonjourDidReceiveMessageNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
        //接收到消息
            let peerID  = notification.userInfo?[BonjourNotificationPeerKey]
            let message = notification.userInfo?[BonjourNotificationMessageKey]
            
        }
        
NSNotificationCenter.defaultCenter().addObserverForName(BonjourDidConnectPeerNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
            //连接到
            let peerID  = notification.userInfo?[BonjourNotificationPeerKey]
            
        }
NSNotificationCenter.defaultCenter().addObserverForName(BonjourDidDisconnectPeerNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (notification) -> Void in
            //失去连接
            let peerID  = notification.userInfo?[BonjourNotificationPeerKey]
            
        }
        

About

communicate with other iOS devices nearby

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages