Skip to content

Commit

Permalink
updated docs about ios 14 updates to local networks and RealityKit co…
Browse files Browse the repository at this point in the history
…mpatibility. also added setDiscoveryInfo to delegate
  • Loading branch information
maxxfrazer committed Jul 18, 2020
1 parent f18ea66 commit e25e05a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/swift-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ on:
jobs:
build:
runs-on: macOS-latest
strategy:
matrix:
SDK: [iphoneos, macosx, appletvos]
steps:
- uses: actions/checkout@v1
- name: build
Expand All @@ -22,4 +25,4 @@ jobs:
env:
PROJECT: MultipeerHelper.xcodeproj
SCHEME: MultipeerHelper-Package
SDK: iphoneos
SDK: ${{ matrix.SDK }}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class RealityViewController: UIViewController, ARSessionDelegate {

extension RealityViewController: MultipeerHelperDelegate {

func shouldSendJoinRequest(_ peer: MCPeerID, with discoveryInfo: [String: String]?) -> Bool {
func shouldSendJoinRequest(
_ peer: MCPeerID,
with discoveryInfo: [String: String]?
) -> Bool {
if self.checkPeerToken(with: discoveryInfo) {
print("compatible peer!")
return true
}
print("incompatible peer!")
Expand Down
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="https://github.com/maxxfrazer/MultipeerHelper/workflows/swiftlint/badge.svg?branch=main"/>
</p>

![Path Example 1](https://github.com/maxxfrazer/MultipeerHelper/blob/main/media/MultipeerHelper-Header.png?raw=true)
![MultipeerHelper Header](https://github.com/maxxfrazer/MultipeerHelper/blob/main/media/MultipeerHelper-Header.png?raw=true)

# MultipeerHelper

Expand All @@ -33,6 +33,17 @@ self.multipeerHelp = MultipeerHelper(
)
```

Because MultipeerConnectivity looks over your local network to find other devices to connect with, there are a few new things to include since iOS 14.

The first thing, is to include the key `NSLocalNetworkUsageDescription` in your app's Info.plist, along with a short text explaining why you need to use the local network. For example "This application needs access to the local network to find opponents.".

As well as the above, you also need to add another key, `NSBonjourServices`. Bonjour services is an array of Bonjour service types.
For example, if your serviceName is "helper-test", you will need to add `_helper-test._tcp` and `_helper-test._udp`.

The two above keys are included in [the Example Project](MultipeerHelper+Example).

### RealityKit

To extend this to RealityKit's synchronization service, simply add the following:

```swift
Expand All @@ -41,6 +52,24 @@ self.arView.scene.synchronizationService = self.multipeerHelp.syncService

And also make sure that your ARConfiguration's isCollaborationEnabled property is set to true.

To make sure RealityKit's synchronizationService runs properly, you must ensure that the RealityKit version installed on any two devices are compatible.

By default, any OS using MultipeerHelper that can install RealityKit (iOS, iPadOS and macOS) will have a key added to the discoveryInfo.
To use this easily, you can add the `shouldSendJoinRequest` method to your `MultipeerHelperDelegate`, and make use of the `checkPeerToken` which is accessible to any class which inherits the `MultipeerHelperDelegate`. Here's an example:

```swift
extension RealityViewController: MultipeerHelperDelegate {
func shouldSendJoinRequest(
_ peer: MCPeerID,
with discoveryInfo: [String: String]?
) -> Bool {
self.checkPeerToken(with: discoveryInfo)
}
}
```

This method is used in [the Example Project](MultipeerHelper+Example).

### Initializer Parameters

#### serviceName
Expand All @@ -51,8 +80,14 @@ This is the type of service to advertise or search for. Due to how MultipeerConn
- Must not begin or end with a hyphen
- Must not contain hyphens adjacent to other hyphens.

#### sessionType (default: .both)
#### sessionType (default: `.both`)
This lets the service know if it should be acting as a service `host` (advertiser), `peer` (browser), or in a scenario where it doesn't matter, `both`. The default for this parameter is `both`, which is the scenario where all devices want to just connect to each other with no questions asked.

#### delegate (default: nil)
#### peerName (default: `UIDevice.current.name`)
String name of your device on the network.

#### encryptionPreference (default: `.required`)
encryptionPreference is how data sent over the network are encrypted.

#### delegate (default: `nil`)
This delegate object will inherit the `MultipeerHelperDelegate` protocol, which can be used for all the handling of transferring data round the network and seeing when others join and leave.
11 changes: 8 additions & 3 deletions Sources/MultipeerHelper/MultipeerHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MultipeerHelper: NSObject {
///
/// `both` creates a session where all users are equal
/// Otherwise if you want one specific user to be the host, choose `host` and `peer`
/// A `host` will create a service advertiser, and a `peer` will create a service browser.
public enum SessionType: Int {
case host = 1
case peer = 2
Expand Down Expand Up @@ -87,14 +88,18 @@ public class MultipeerHelper: NSObject {
session.delegate = self

if (self.sessionType.rawValue & SessionType.host.rawValue) != 0 {
var discoveryInfo = [String: String]()
if #available(iOS 13.4, *) {
var discoveryInfo = self.delegate?.setDiscoveryInfo?()
?? [String: String]()

#if canImport(RealityKit)
if #available(iOS 13.4, macOS 10.15.4, *) {
let networkLoc = NetworkCompatibilityToken.local
let jsonData = try? JSONEncoder().encode(networkLoc)
if let encodedToken = String(data: jsonData!, encoding: .utf8) {
discoveryInfo["compatability_token"] = encodedToken
discoveryInfo["compatibility_token"] = encodedToken
}
}
#endif
serviceAdvertiser = MCNearbyServiceAdvertiser(
peer: myPeerID,
discoveryInfo: discoveryInfo,
Expand Down
19 changes: 12 additions & 7 deletions Sources/MultipeerHelper/MultipeerHelperDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import MultipeerConnectivity
/// - Returns: Bool if the peer's join request should be accepted
@objc optional func shouldAcceptJoinRequest(peerID: MCPeerID, context: Data?) -> Bool

/// This will be set as the base for the discoveryInfo, which is sent out by the advertiser (host).
/// The key "compatibility_token" is in use by MultipeerHelper, for checking the
/// compatibility of RealityKit versions.
@objc optional func setDiscoveryInfo() -> [String: String]

/// Peer can no longer be found on the network, and thus cannot receive data
/// - Parameter peer: If a peer has left the network in a non typical way
@objc optional func peerLost(_ peer: MCPeerID)
Expand All @@ -48,23 +53,23 @@ import MultipeerConnectivity
#if canImport(RealityKit)
import RealityKit
extension MultipeerHelperDelegate {
/// Checks whether the discovered session is using a compabible version of RealityKit
/// Checks whether the discovered session is using a compatible version of RealityKit
/// For collaborative sessions.
/// - Parameter discoveryInfo:
/// - Returns:
/// - Parameter discoveryInfo: The discoveryInfo from the advertiser
/// picked up by a browser.
/// - Returns: Boolean representing whether or not the two devices
/// have compatible versions of RealityKit.
public func checkPeerToken(with discoveryInfo: [String: String]?) -> Bool {
guard let compTokenStr = discoveryInfo?["compatability_token"]
guard let compTokenStr = discoveryInfo?["compatibility_token"]
else {
return false
}
print("Found token key in discovery info")
if #available(iOS 13.4, *) {
if #available(iOS 13.4, macOS 10.15.4, *) {
if let tokenData = compTokenStr.data(using: .utf8),
let compToken = try? JSONDecoder().decode(
NetworkCompatibilityToken.self,
from: tokenData
) {
print("Decoded Token")
return compToken.compatibilityWith(.local) == .compatible
}
}
Expand Down

0 comments on commit e25e05a

Please sign in to comment.