Keep only one endpoint id

This commit is contained in:
Leon Böttger committed 2025-04-04 20:35:42 +02:00
1 parent ce77e73428
commit b81a453b69
2 files changed
+23 -8

No files matched your search

+1
View File
@@ -18,4 +18,5 @@ public enum UserDefaultsKeys: String, CaseIterable {
case transmissionCount = "reviewRequestCountKey"
case automaticallyAcceptFiles = "automaticallyAcceptFiles"
case saveFolderBookmark = "saveFolderBookmark"
case endpointID = "endpointID"
}
+22 -8
View File
@@ -189,14 +189,28 @@ public class NearbyConnectionManager : NSObject, NetServiceDelegate, InboundNear
tcpListener.start(queue: .global(qos: .utility))
}
private static func generateEndpointID()->[UInt8]{
var id:[UInt8]=[]
let alphabet="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".compactMap {UInt8($0.asciiValue!)}
for _ in 0...3{
id.append(alphabet[Int.random(in: 0..<alphabet.count)])
}
return id
}
private static func generateEndpointID() -> [UInt8] {
let userDefaultsKey = UserDefaultsKeys.endpointID.rawValue
// Try to retrieve from UserDefaults
if let savedString = UserDefaults.standard.string(forKey: userDefaultsKey),
let savedData = savedString.data(using: .utf8) {
return [UInt8](savedData)
}
// Generate a new random ID
var id: [UInt8] = []
let alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".compactMap { UInt8($0.asciiValue!) }
for _ in 0..<4 { // use 0..<4 instead of 0...3 for clarity
id.append(alphabet[Int.random(in: 0..<alphabet.count)])
}
// Save to UserDefaults as String
let idString = String(bytes: id, encoding: .utf8) ?? ""
UserDefaults.standard.set(idString, forKey: userDefaultsKey)
return id
}
private func initMDNS(){
let nameBytes:[UInt8]=[