1.github上的demo
insidegui/MultipeerDemo – github
insidegui/MultipeerKit – github(我用的是这个写我的项目)
2.特别说明:Info.plist 要求
Info.plist 要求
从 iOS 14 开始,为了使 MultipeerKit 正常工作,您需要在应用的 Info.plist 文件中包含两个关键字。
这些键是 Privacy - Local Network Usage Description (NSLocalNetworkUsageDescription) 和 Bonjour services (NSBonjourServices)。
对于隐私键,包括一个人类可读的描述,说明用户允许您的应用访问本地网络上的设备的好处是什么。
Bonjour 服务键是您的应用将要浏览的服务类型的数组。对于 MultipeerKit,该条目的格式应为 _servicename._tcp,其中 servicename 是您在 MultipeerConfiguration 中设置的 serviceType。如果您使用的是默认配置,此键的值应为 _MKSVC._tcp。
3.1 info.plist没有配置正确会报-72008的错误
2023-08-24 15:16:45.917165+0800 RemoteCamera[5294:3939055] [MultipeerConnection] resume()
2023-08-24 15:16:45.923267+0800 RemoteCamera[5294:3939256] [MCNearbyServiceBrowser] NSNetServiceBrowser did not search with error dict [{
NSNetServicesErrorCode = "-72008";
NSNetServicesErrorDomain = 10;
}].
2023-08-24 15:16:45.923357+0800 RemoteCamera[5294:3939255] [MCNearbyServiceAdvertiser] Server did not publish: errorDict [{
NSNetServicesErrorCode = "-72008";
NSNetServicesErrorDomain = 10;
}].
2023-08-24 15:16:45.923403+0800 RemoteCamera[5294:3939055] [MultipeerConnection] The multipeer connection failed to start browsing for peers. This could be due to missing keys in your app's Info.plist, check out the documentation at http://github.com/insidegui/MultipeerKit for more information. Error: Error Domain=NSNetServicesErrorDomain Code=-72008 "(null)"
2023-08-24 15:16:45.923508+0800 RemoteCamera[5294:3939055] [MultipeerConnection] The multipeer connection failed to start advertising to peers. This could be due to missing keys in your app's Info.plist, check out the documentation at http://github.com/insidegui/MultipeerKit for more information. Error: Error Domain=NSNetServicesErrorDomain Code=-72008 "(null)"
3.我用MultipeerKit写我自己的demo测试成功
3.1 发数据界面
3.2 收数据界面
文章来源:https://www.toymoban.com/news/detail-666111.html
3.3 代码
func printXY(_ any:Any,obj:Any,line:Int) {
#if DEBUG
let date = Date()
let timeFormatter = DateFormatter()
//日期显示格式,可按自己需求显示
timeFormatter.dateFormat = "HH:mm:ss.SSS"
let strNowTime = timeFormatter.string(from: date) as String
print("\(strNowTime) \(type(of: obj)) \(line): \(any)")
#endif
}
import UIKit
import MultipeerKit
import Combine
struct ExamplePayload: Hashable, Codable {
let message: String
}
class ViewController: UIViewController{
private lazy var transceiver: MultipeerTransceiver = {
var config = MultipeerConfiguration.default
config.serviceType = "uservc"
//加了这一句代码availablePeers会自动连接,不加就不会连接,发不了数据
config.security.encryptionPreference = .required
let t = MultipeerTransceiver(configuration: config)
t.receive(ExamplePayload.self) { [weak self] payload,arg in
printXY("收到的数据:\(payload)", obj: self, line: #line)
self?.receivedDataLabel.text = payload.message
}
return t
}()
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var receivedDataLabel: UILabel!
var dataSource: MultipeerDataSource!
var cancellable: AnyCancellable?
override func viewDidLoad() {
super.viewDidLoad()
dataSource = MultipeerDataSource(transceiver: transceiver)
// 订阅 availablePeers 的变化
cancellable = dataSource.$availablePeers.sink { newPeers in
// print("New available peers: \(newPeers)")
printXY("New available peers: \(newPeers)", obj: self, line: #line)
// 在这里你可以更新 UI,例如刷新一个表视图
}
transceiver.resume()
}
deinit {
// 取消订阅
cancellable?.cancel()
}
@IBAction func connectDevices() {
}
@IBAction func sendMessage() {
let payload = ExamplePayload(message: textField.text ?? "123")
printXY("发送的数据:\(payload)", obj: self, line: #line)
dataSource.transceiver.send(payload, to: dataSource.availablePeers)
}
}
相关博客:
MultipeerConnectivity框架详细解析(二) —— 一个简单示例(一) 简书文章来源地址https://www.toymoban.com/news/detail-666111.html
到了这里,关于iOS swift 类似AirDrop的近场数据传输 MultipeerConnectivity 框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!