我想以编程方式在iOS中更改/添加我的iPhone设备连接WiFi的DNS服务器吗?
Google公共DNS IP地址(IPv4)如下: 8.8.8.8 8.8.4.4
我想以编程方式更改/添加它,就像在附加的屏幕截图中一样。
请帮忙。我可以在Objective-c或Swift中管理它。在Objective-c中对我更有帮助。 提前谢谢。
连接Wi-Fi的虚拟/旧DNS服务器: -
连接的Wi-Fi所需的DNS服务器: -
答案 0 :(得分:4)
查看Network Extensions类。使用NETunnelProviderManager课程,您可以使用onDemandRules设置NEEvaluateConnectionRule。 NEEvaluateConnectionRule constructor可以将带有顶级域名(即["*.com", "*.net", "*.org", "*.io"]
)的通配符列表作为域名,并使用NEEvaluateConnectionRuleAction.connectIfNeeded作为操作。将您创建的onDemandRules的NEEvaluateConnectionRule设置为所有tlds作为域。然后创建NEOnDemandRuleEvaluateConnection并将其connectionRules设置为使用所有顶级域创建的NEEvaluateConnectionRule,并将其interfaceTypeMatch设置为NEOnDemandRuleInterfaceType.any。将NETunnelProviderManager.onDemandRules设置为以此方式创建的NEOnDemandRuleEvaluateConnection。如果您如上所述创建了NETunnelProviderManager和load it以及save it,则可以使用NETunnelProviderManager.isEnabled和NETunnelProviderManager.isOnDemandEnabled属性打开和关闭它。< / p>
这是一个完全相同的示例类。
import Foundation
import NetworkExtension
public class VPNConnect {
private static let vpnDescription = "DNS OnDemand to GoogleDNS"
private static let vpnServerDescription = "OnDemand DNS to GoogleDNS"
public var manager:NETunnelProviderManager = NETunnelProviderManager()
public var dnsEndpoint1:String = "8.8.8.8"
public var dnsEndpoint2:String = "8.8.4.4"
public var connected:Bool {
get {
return self.manager.isOnDemandEnabled
}
set {
if newValue != self.connected {
update(
body: {
self.manager.isEnabled = newValue
self.manager.isOnDemandEnabled = newValue
},
complete: {
if newValue {
do {
try (self.manager.connection as? NETunnelProviderSession)?.startVPNTunnel(options: nil)
} catch let err as NSError {
NSLog("\(err.localizedDescription)")
}
} else {
(self.manager.connection as? NETunnelProviderSession)?.stopVPNTunnel()
}
}
)
}
}
}
public init() {
refreshManager()
}
public func refreshManager() -> Void {
NETunnelProviderManager.loadAllFromPreferences(completionHandler: { (managers, error) in
if nil == error {
if let managers = managers {
for manager in managers {
if manager.localizedDescription == VPNConnect.vpnDescription {
self.manager = manager
return
}
}
}
}
self.setPreferences()
})
}
private func update(body: @escaping ()->Void, complete: @escaping ()->Void) {
manager.loadFromPreferences { error in
if (error != nil) {
NSLog("Load error: \(String(describing: error?.localizedDescription))")
return
}
body()
self.manager.saveToPreferences { (error) in
if nil != error {
NSLog("vpn_connect: save error \(error!)")
} else {
complete()
}
}
}
}
private func setPreferences() {
self.manager.localizedDescription = VPNConnect.vpnDescription
let proto = NETunnelProviderProtocol()
proto.providerBundleIdentifier = "com.popmedic.vpntunnel.provider"
proto.serverAddress = VPNConnect.vpnServerDescription
self.manager.protocolConfiguration = proto
// TLDList is a struct I created in its own swift file that has an array of all top level domains
let evaluationRule = NEEvaluateConnectionRule(matchDomains: TLDList.tlds,
andAction: NEEvaluateConnectionRuleAction.connectIfNeeded)
evaluationRule.useDNSServers = [self.dnsEndpoint1, self.dnsEndpoint2]
let onDemandRule = NEOnDemandRuleEvaluateConnection()
onDemandRule.connectionRules = [evaluationRule]
onDemandRule.interfaceTypeMatch = NEOnDemandRuleInterfaceType.any
self.manager.onDemandRules = [onDemandRule]
}
}
请注意,您必须打开网络扩展功能,然后会出现一个对话框,告诉用户您正在打开VPN连接,但当状态栏中没有[VPN]图标时连接已打开,因为我们没有设置vpn,只使用按需规则。
Here is the TLDList class I used.
尽可能多地讨厌谷歌,也许将此用于您设置的DNS ... Quad9