KYNearbyService 1.2.0

KYNearbyService 1.2.0

Kjuly 维护。




KYNearbyService

用于本地发现和通信的服务。

macOS iOS SwiftPM

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)