fix: relaunching of iOS app would stop advertising

This commit is contained in:
Leon Böttger committed 2025-08-18 10:36:31 +02:00
1 parent 9d71ed35b7
commit 7d6d0f3482
6 files changed
+66 -14

No files matched your search

@@ -29,6 +29,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
private var shareExtensionDelegates: [OutboundAppDelegate] = []
private var outgoingTransfers: [String: OutgoingTransferInfo] = [:]
private var startedDeviceDiscovery = false
private var startedAdvertising = false
private var browsers: [NWBrowser] = []
private let serviceTypes = ["_FC9F5ED42C8A._tcp."]
private var qrCodePrivateKey: ECPrivateKey?
@@ -56,11 +57,16 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
public func becomeVisible() {
startTCPListener()
}
private func startTCPListener() {
if startedAdvertising {
log("[NearbyConnectionManager] Already advertising, skipping")
return
}
log("[NearbyConnectionManager] Becoming visible")
tcpListener = try! NWListener(using: NWParameters(tls: .none))
startedAdvertising = true
tcpListener.stateUpdateHandler = { (state: NWListener.State) in
if case .ready = state {
self.initMDNS()
@@ -77,6 +83,20 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
}
public func becomeInvisible() {
if !startedAdvertising {
print("[NearbyConnectionManager] Already invisible, ignoring becomeInvisible()")
return
}
log("[NearbyConnectionManager] Becoming invisible")
startedAdvertising = false
self.stopMDNS()
tcpListener.cancel()
}
private static func generateEndpointID() -> [UInt8] {
let userDefaultsKey = UserDefaultsKeys.endpointID.rawValue
@@ -125,6 +145,15 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
}
private func stopMDNS() {
// Gracefully stop each published service
for service in mdnsServices {
service.stop()
}
mdnsServices.removeAll()
}
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
mainAppDelegate?.obtainUserConsent(for: transfer, from: device)
}
@@ -188,7 +217,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
public func stopDeviceDiscovery() {
if startedDeviceDiscovery {
log("Stopping device discovery")
log("[NearbyConnectionManager] Stopping device discovery")
for browser in browsers {
browser.cancel()
@@ -196,6 +225,15 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
browsers.removeAll()
startedDeviceDiscovery = false
for delegate in shareExtensionDelegates {
foundServices.values.forEach { service in
guard let device = service.device, let id = device.id else { return }
delegate.removeDevice(id: id)
}
}
}
}
+1 -7
View File
@@ -24,14 +24,8 @@ final class LogManager {
/// Private initializer
private init() {
#if os(macOS)
// Use the App Group container
let directory = getAppGroupDirectory()
#else
// On iOS, use the library directory, to not let the logs appear in the files app
let directory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first
#endif
// Create file name from date
let formatter = DateFormatter()
@@ -133,7 +127,7 @@ public func log(_ text: String) {
/// Returns the shared App Group directory
fileprivate func getAppGroupDirectory() -> URL? {
func getAppGroupDirectory() -> URL? {
let groupIdentifier = "group.com.leonboettger.neardrop"
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)
}