https://github.com/kjuly/kynearbyservice
A service for nearby discovery and communication.
https://github.com/kjuly/kynearbyservice
airdrop bluetooth bonjour ios local-network mac-catalyst macos multipeer-connectivity swift swift-ui
Last synced: about 1 month ago
JSON representation
A service for nearby discovery and communication.
- Host: GitHub
- URL: https://github.com/kjuly/kynearbyservice
- Owner: Kjuly
- License: mit
- Created: 2023-11-29T03:42:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-04T08:54:23.000Z (3 months ago)
- Last Synced: 2025-03-23T10:51:08.161Z (about 1 month ago)
- Topics: airdrop, bluetooth, bonjour, ios, local-network, mac-catalyst, macos, multipeer-connectivity, swift, swift-ui
- Language: Swift
- Homepage:
- Size: 85 KB
- Stars: 13
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# KYNearbyService
A service for nearby discovery and communication.
[](https://swiftpackageindex.com/Kjuly/KYNearbyService)
[](https://swiftpackageindex.com/Kjuly/KYNearbyService)
![macOS][macOS-Badge] ![iOS][iOS-Badge]
[![SPM][SPM-Badge]][SPM-Link] [![CocoaPods][CocoaPods-Badge]][CocoaPods-Link] [![Carthage][Carthage-Badge]][Carthage-Link][macOS-Badge]: https://img.shields.io/badge/macOS-12.0%2B-blue?labelColor=00367A&color=3081D0
[iOS-Badge]: https://img.shields.io/badge/iOS-15.5%2B-blue?labelColor=00367A&color=3081D0[SPM-Badge]: https://img.shields.io/github/v/tag/Kjuly/KYNearbyService?label=SPM&labelColor=2F4858&color=A8DF8E
[SPM-Link]: https://swiftpackageindex.com/Kjuly/KYNearbyService
[CocoaPods-Badge]: https://img.shields.io/cocoapods/v/KYNearbyService?label=CocoaPods&labelColor=2F4858&color=A8DF8E
[CocoaPods-Link]: https://cocoapods.org/pods/KYNearbyService
[Carthage-Badge]: https://img.shields.io/github/v/tag/Kjuly/KYNearbyService?label=Carthage&labelColor=2F4858&color=A8DF8E
[Carthage-Link]: https://swiftpackageindex.com/Kjuly/KYNearbyService![]()
![]()
## Installation
See the following subsections for details on the different installation methods.
- [Swift Package Manager](INSTALLATION.md#swift-package-manager)
- [CocoaPods](INSTALLATION.md#cocoaPods)
- [Carthage](INSTALLATION.md#carthage)
- [Submodule](INSTALLATION.md#submodule)## Usage
1. Setup KYNearbyService with your service type.
```Swift
KYNearbyService.setup(with: KYNearbyServiceConfiguration(serviceType: "your-service")
```> [!IMPORTANT]
> Make sure you've provided [NSBonjourServices](https://developer.apple.com/documentation/bundleresources/information_property_list/nsbonjourservices) in your *.plist file.
>
> ```xml
> NSBonjourServices
>
> _your-service._tcp
> _your-service._udp
>
> ```2. Use the existing `KYNearbyConnectionView` or setup your own one to provide as the connection view. A demo project is available under "[/KYNearbyServiceDemo](KYNearbyServiceDemo)".
3. Observe notifications (`Notification.Name.KYNearbyService.*`) to handle events.
| Notification | When | Notes
| --- | --- | ---
| didUpdatePeerDisplayName | The user changed the display name | The name is provided as `note.object`.
| shouldSendResource | The user pressed the "SEND" button | The target peer item (KYNearbyPeerModel instance) is provided as `note.object`. And you can use `KYNearbyService.sendResource(for:at:withName:completion:)` to send the resource to the target peer.
| didStartReceivingResource | The service start receiving resource |
| didReceiveResource | The service did receive resource | The details are available in `note.userInfo`. And the file will be saved to `KYNearbyServiceDefaultFolderURL.archives` by default. You can config the destination folder url by `KYNearbyServiceConfiguration`.e.g.
```Swift
NotificationCenter.default.addObserver(
self,
selector: #selector(_handleKYNearbyServiceShouldSendResourceNotification),
name: .KYNearbyService.shouldSendResource,
object: nil)
```## All Peer Status Preview
![]()
![]()
You can go to the demo project's `KYNearbyServiceDemoApp.init()`, and switch `.none` to `allPeerStatuses` to get a list of all peer statuses:
```swift
KYNearbyService.shared.debug_populateMockPeers(for: .allPeerStatuses)
```