mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-24 23:36:05 +00:00
fix: relaunching of iOS app would stop advertising
This commit is contained in:
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 shareExtensionDelegates: [OutboundAppDelegate] = []
|
||||||
private var outgoingTransfers: [String: OutgoingTransferInfo] = [:]
|
private var outgoingTransfers: [String: OutgoingTransferInfo] = [:]
|
||||||
private var startedDeviceDiscovery = false
|
private var startedDeviceDiscovery = false
|
||||||
|
private var startedAdvertising = false
|
||||||
private var browsers: [NWBrowser] = []
|
private var browsers: [NWBrowser] = []
|
||||||
private let serviceTypes = ["_FC9F5ED42C8A._tcp."]
|
private let serviceTypes = ["_FC9F5ED42C8A._tcp."]
|
||||||
private var qrCodePrivateKey: ECPrivateKey?
|
private var qrCodePrivateKey: ECPrivateKey?
|
||||||
@@ -56,11 +57,16 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
|
|
||||||
|
|
||||||
public func becomeVisible() {
|
public func becomeVisible() {
|
||||||
startTCPListener()
|
if startedAdvertising {
|
||||||
}
|
log("[NearbyConnectionManager] Already advertising, skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
private func startTCPListener() {
|
|
||||||
|
log("[NearbyConnectionManager] Becoming visible")
|
||||||
|
|
||||||
|
tcpListener = try! NWListener(using: NWParameters(tls: .none))
|
||||||
|
startedAdvertising = true
|
||||||
|
|
||||||
tcpListener.stateUpdateHandler = { (state: NWListener.State) in
|
tcpListener.stateUpdateHandler = { (state: NWListener.State) in
|
||||||
if case .ready = state {
|
if case .ready = state {
|
||||||
self.initMDNS()
|
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] {
|
private static func generateEndpointID() -> [UInt8] {
|
||||||
let userDefaultsKey = UserDefaultsKeys.endpointID.rawValue
|
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) {
|
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
|
||||||
mainAppDelegate?.obtainUserConsent(for: transfer, from: device)
|
mainAppDelegate?.obtainUserConsent(for: transfer, from: device)
|
||||||
}
|
}
|
||||||
@@ -188,7 +217,7 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
public func stopDeviceDiscovery() {
|
public func stopDeviceDiscovery() {
|
||||||
if startedDeviceDiscovery {
|
if startedDeviceDiscovery {
|
||||||
|
|
||||||
log("Stopping device discovery")
|
log("[NearbyConnectionManager] Stopping device discovery")
|
||||||
|
|
||||||
for browser in browsers {
|
for browser in browsers {
|
||||||
browser.cancel()
|
browser.cancel()
|
||||||
@@ -196,6 +225,15 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
|||||||
|
|
||||||
browsers.removeAll()
|
browsers.removeAll()
|
||||||
startedDeviceDiscovery = false
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,14 +24,8 @@ final class LogManager {
|
|||||||
|
|
||||||
/// Private initializer
|
/// Private initializer
|
||||||
private init() {
|
private init() {
|
||||||
|
|
||||||
#if os(macOS)
|
|
||||||
// Use the App Group container
|
// Use the App Group container
|
||||||
let directory = getAppGroupDirectory()
|
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
|
// Create file name from date
|
||||||
let formatter = DateFormatter()
|
let formatter = DateFormatter()
|
||||||
@@ -133,7 +127,7 @@ public func log(_ text: String) {
|
|||||||
|
|
||||||
|
|
||||||
/// Returns the shared App Group directory
|
/// Returns the shared App Group directory
|
||||||
fileprivate func getAppGroupDirectory() -> URL? {
|
func getAppGroupDirectory() -> URL? {
|
||||||
let groupIdentifier = "group.com.leonboettger.neardrop"
|
let groupIdentifier = "group.com.leonboettger.neardrop"
|
||||||
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)
|
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@
|
|||||||
"location" : "https://github.com/leonboe1/LUI.git",
|
"location" : "https://github.com/leonboe1/LUI.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"branch" : "main",
|
"branch" : "main",
|
||||||
"revision" : "b4a9766db0f328c8251b1fa8bed2472c783d7128"
|
"revision" : "0fa79d937a5948c59f44c64b12af21c7d3b408e5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ let configuration = LUIConfiguration(
|
|||||||
|
|
||||||
copyrightInformation: licenseText,
|
copyrightInformation: licenseText,
|
||||||
|
|
||||||
|
loggingUrl: getAppGroupDirectory(),
|
||||||
|
|
||||||
introductionViewInformation: "QuickDropWelcomeDescription",
|
introductionViewInformation: "QuickDropWelcomeDescription",
|
||||||
|
|
||||||
usesAutomaticReviewPrompt: false,
|
usesAutomaticReviewPrompt: false,
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import LUI
|
|||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
|
||||||
|
@Environment(\.scenePhase) var scenePhase
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
AppRootView(isPlus: .constant(true), phoneView: {
|
AppRootView(isPlus: .constant(true), phoneView: {
|
||||||
DeviceListView()
|
DeviceListView()
|
||||||
@@ -23,6 +25,21 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.onChange(of: scenePhase) { newValue in
|
||||||
|
if newValue == .active {
|
||||||
|
#if !EXTENSION
|
||||||
|
NearbyConnectionManager.shared.becomeVisible()
|
||||||
|
#endif
|
||||||
|
NearbyConnectionManager.shared.startDeviceDiscovery()
|
||||||
|
}
|
||||||
|
|
||||||
|
if newValue == .background {
|
||||||
|
#if !EXTENSION
|
||||||
|
NearbyConnectionManager.shared.becomeInvisible()
|
||||||
|
#endif
|
||||||
|
NearbyConnectionManager.shared.stopDeviceDiscovery()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class ShareViewController: UIViewController {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// skip introduction for share extension
|
// skip introduction for share extension
|
||||||
|
LUIInit(configuration: configuration)
|
||||||
LUISettings.sharedInstance.appLaunchedBefore = true
|
LUISettings.sharedInstance.appLaunchedBefore = true
|
||||||
|
|
||||||
// Replace with your app’s SwiftUI entry view
|
// Replace with your app’s SwiftUI entry view
|
||||||
|
|||||||
Reference in new issue
Block a user