KYNearbyService

一个用于附近设备发现和通信的服务。


macOS iOS
SPM CocoaPods Carthage

iPhone Preview Mac Preview

安装

请参阅以下子章节,了解不同安装方法的详细信息。

用法

  1. 使用您的服务类型设置 KYNearbyService。
KYNearbyService.setup(with: KYNearbyServiceConfiguration(serviceType: "your-service")

重要提示

请确保您已在 *.plist 文件中提供 NSBonjourServices

<key>NSBonjourServices</key>
<array>
  <string>_your-service._tcp</string>
  <string>_your-service._udp</string>
</array>
  1. 使用现有的 KYNearbyConnectionView 或设置您自己的一个作为连接视图。演示项目可在 "/KYNearbyServiceDemo" 下找到。

  2. 观察通知 (Notification.Name.KYNearbyService.*) 以处理事件。

通知 何时 备注
didUpdatePeerDisplayName (对等方显示名称已更新) 用户更改了显示名称 该名称作为 note.object 提供。
shouldSendResource (应发送资源) 用户按下了“发送”按钮 目标对等方项目(KYNearbyPeerModel 实例)作为 note.object 提供。您可以使用 KYNearbyService.sendResource(for:at:withName:completion:) 将资源发送到目标对等方。
didStartReceivingResource (开始接收资源) 服务开始接收资源
didReceiveResource (已接收资源) 服务已接收资源 详细信息可在 note.userInfo 中找到。默认情况下,该文件将保存到 KYNearbyServiceDefaultFolderURL.archives。您可以使用 KYNearbyServiceConfiguration 配置目标文件夹 URL。

例如:

NotificationCenter.default.addObserver(
  self,
  selector: #selector(_handleKYNearbyServiceShouldSendResourceNotification),
  name: .KYNearbyService.shouldSendResource,
  object: nil)

所有对等方状态预览

iPhone Preview iPhone Preview

您可以进入演示项目的 KYNearbyServiceDemoApp.init(),并将 .none 切换为 allPeerStatuses 以获取所有对等方状态的列表

KYNearbyService.shared.debug_populateMockPeers(for: .allPeerStatuses)