diff --git a/Sources/MultipeerHelper/MultipeerHelper.swift b/Sources/MultipeerHelper/MultipeerHelper.swift index 16945f2..d4d3ebe 100644 --- a/Sources/MultipeerHelper/MultipeerHelper.swift +++ b/Sources/MultipeerHelper/MultipeerHelper.swift @@ -6,6 +6,7 @@ // import MultipeerConnectivity +import Foundation #if canImport(RealityKit) import RealityKit #endif @@ -22,6 +23,10 @@ public class MultipeerHelper: NSObject { case both = 3 } + public static let compTokenKey = "MPH_CompToken" + public static let osVersionKey = "MPH_OSVersion" + public static let platformKey = "MPH_Platform" + /// Detemines whether your service is advertising, browsing, or both. public let sessionType: SessionType public let serviceName: String @@ -86,7 +91,10 @@ public class MultipeerHelper: NSObject { encryptionPreference: encryptionPreference ) session.delegate = self + self.setupSession() + } + private func setupSession() { if (self.sessionType.rawValue & SessionType.host.rawValue) != 0 { var discoveryInfo = self.delegate?.setDiscoveryInfo?() ?? [String: String]() @@ -96,10 +104,21 @@ public class MultipeerHelper: NSObject { let networkLoc = NetworkCompatibilityToken.local let jsonData = try? JSONEncoder().encode(networkLoc) if let encodedToken = String(data: jsonData!, encoding: .utf8) { - discoveryInfo["compatibility_token"] = encodedToken + discoveryInfo[MultipeerHelper.compTokenKey] = encodedToken } } #endif + #if os(iOS) || os(tvOS) + discoveryInfo[MultipeerHelper.osVersionKey] = UIDevice.current.systemVersion + #if os(iOS) + discoveryInfo[MultipeerHelper.platformKey] = "iOS" + #else + discoveryInfo[MultipeerHelper.platformKey] = "tvOS" + #endif + #elseif os(macOS) + discoveryInfo[MultipeerHelper.osVersionKey] = ProcessInfo.processInfo.operatingSystemVersionString + discoveryInfo[MultipeerHelper.platformKey] = "macOS" + #endif serviceAdvertiser = MCNearbyServiceAdvertiser( peer: myPeerID, discoveryInfo: discoveryInfo, @@ -176,12 +195,12 @@ public class MultipeerHelper: NSObject { /// Method used for disconnecting all services. Once completed, /// create a new MultipeerHelper if you want to connect to sessions again. - func disconnectAll() { + public func disconnectAll() { self.serviceAdvertiser?.stopAdvertisingPeer() self.serviceBrowser?.stopBrowsingForPeers() self.serviceAdvertiser = nil self.serviceBrowser = nil - self.session.disconnect() + self.session?.disconnect() } } diff --git a/Sources/MultipeerHelper/MultipeerHelperDelegate.swift b/Sources/MultipeerHelper/MultipeerHelperDelegate.swift index 79d2790..83038d5 100644 --- a/Sources/MultipeerHelper/MultipeerHelperDelegate.swift +++ b/Sources/MultipeerHelper/MultipeerHelperDelegate.swift @@ -37,7 +37,7 @@ import MultipeerConnectivity @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 + /// The key "MultipeerHelper.compTokenKey" is in use by MultipeerHelper, for checking the /// compatibility of RealityKit versions. @objc optional func setDiscoveryInfo() -> [String: String] @@ -60,7 +60,7 @@ extension MultipeerHelperDelegate { /// - Returns: Boolean representing whether or not the two devices /// have compatible versions of RealityKit. public static func checkPeerToken(with discoveryInfo: [String: String]?) -> Bool { - guard let compTokenStr = discoveryInfo?["compatibility_token"] + guard let compTokenStr = discoveryInfo?[MultipeerHelper.compTokenKey] else { return false }