Skip to content

Instantly share code, notes, and snippets.

@ZChronoss
Last active December 22, 2025 13:06
Show Gist options
  • Select an option

  • Save ZChronoss/0fc41fdaf31e6b4c0dac90078e1e6568 to your computer and use it in GitHub Desktop.

Select an option

Save ZChronoss/0fc41fdaf31e6b4c0dac90078e1e6568 to your computer and use it in GitHub Desktop.
class DOHService {
static func setupDoH() {
// change the server url to your doh server's url
let serverUrl = "doh.example.com"
let manager = NEDNSSettingsManager.shared()
manager.loadFromPreferences { loadError in
// handles error
if let loadError = loadError {
print("Failed to load DoH preferences: \(loadError)")
return
}
// Configure DoH settings
// servers here means a fallback ip address
// so if your doh is failing, it will fallback to this ip
let dohSettings = NEDNSOverHTTPSSettings(servers: [])
// put your server's url here
dohSettings.serverURL = URL(string: serverUrl)
// match domains means which domain should apply your doh settings
// you can put some domains here, empty means it applies to all domains
dohSettings.matchDomains = [] // Matches all domains
manager.dnsSettings = dohSettings
// Enable on-demand rule (default OFF)
// it is up to you if you want to turn it on or off right off the bat
let rule = NEOnDemandRuleDisconnect()
// the interface type match is any because i want to apply it to all types of connection. You can choose wifi only or cellular only here
rule.interfaceTypeMatch = .any
manager.onDemandRules = [
rule,
]
// Save
// after this, your app's dns settings should appear in system settings. You can see this by navigating to Settings app -> General -> VPN & Device Management -> DNS
manager.saveToPreferences { saveError in
if let saveError = saveError {
print("Failed to save DoH preferences: \(saveError.localizedDescription)")
} else {
print("Successfully saved DoH preferences")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment