DNSServiceProcessResult users complaining about app freeze

I am using DNSServiceBrowse for getting the kDNSServiceErr_PolicyDenied (-65570) error for local network access as there is no API as per https://developer.apple.com/forums/thread/663852

I released the app on AppStore but now some users are complaining about app freeze issues and eventually app crash.
I am unable to reproduce this issue on my device at all. But AppStore reviews are filled with "app freeze" and "crashes" keywords.


Code Block
let pointer: UnsafePointer<Int8>? = NSString(string: "_services._dns-sd._udp").utf8String
var browseRef: DNSServiceRef?
DNSServiceBrowse(&browseRef, 0, 0, pointer, nil, browseCallback, nil)
DispatchQueue.global().async {
DNSServiceProcessResult(browseRef)
DNSServiceRefDeallocate(browseRef)
}


Any suggestions about how to debug this?
The DNS-SD API is not the easiest thing to use. Have you tried using a higher-level API? You should get the same error propagated up there too.

If you stick with DNS-SD, you’ll need to fix your code. Using a Dispatch global queue is a bad idea in general and a particularly bad idea in this case (see this post for some details as to why). For an example of how to use this API correctly, see the DNSSDObjects sample code.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
Thank you eskimo for the response. Can you share some high-level APIs that can provide a response for local network access permission?

I changed the code to be more in line with the documentation for DNSServiceProcessResult and DNSServiceBrowse

What do you think?

Code Block
let startTime = Date()
let pointer: UnsafePointer<Int8>? = NSString(string: "_services._dns-sd._udp").utf8String
var browseRef: DNSServiceRef?
DNSServiceBrowse(&browseRef, 0, 0, pointer, nil, self?.browseCallback, nil)
let socket = DNSServiceRefSockFD(browseRef)
let readSource = DispatchSource.makeReadSource(fileDescriptor: socket, queue: DispatchQueue(label: "com.x.y.z"))
readSource.setEventHandler() {
DNSServiceProcessResult(browseRef)
DNSServiceRefDeallocate(browseRef)
}
readSource.resume()
repeat {
RunLoop.current.run(mode: .default, before: NSDate.distantFuture)
} while (Date().timeIntervalSince(startTime) <= TimeInterval(5)); // Waits for 5 seconds and then proceeds


Can you share some high-level APIs that can provide a response for
local network access permission?

If you want to stay in the Bonjour space, check out NWBrowser. If you start the browser and local network privacy is denied, it’ll transition to the .waiting(_:) state with an kDNSServiceErr_PolicyDenied error. You can detect that with code like the following:

Code Block
switch state {
case .waiting(let error):
if let nwError = error as? NWError, case .dns(let code) = nwError, code == kDNSServiceErr_PolicyDenied {
… unauthorised …
} else {
… something else …
}
}


What do you think?

I think you need to stay away from DNS-SD; it’s not an easy API to use correctly.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
DNSServiceProcessResult users complaining about app freeze
 
 
Q