如何使用swift更改wifi DNS设置iOS

问题描述 投票:0回答:1

我正在尽我所能让这个工作。

我的客户端有一个DNS服务器,有时我会发送电子邮件给他们的DNS设置。但现在我想通过ios应用程序做到这一点。有没有办法让他们下载应用程序并点击应用程序上的一个按钮来完成它?

我看着这个https://developer.apple.com/library/ios/documentation/NetworkExtension/Reference/NEDNSSettingsClassRef/index.html#//apple_ref/occ/cl/NEDNSSettings

但不确定

谢谢

ios swift networking dns settings
1个回答
0
投票

查看Network Extensions类。使用NETunnelProviderManager类,你可以用onDemandRules设置NEEvaluateConnectionRuleNEEvaluateConnectionRule constructor可以列出带有顶级域(即["*.com", "*.net", "*.org", "*.io"])的通配符作为域,并使用NEEvaluateConnectionRuleAction.connectIfNeeded作为动作。设置您创建的onDemandRulesNEEvaluateConnectionRule,将所有tld作为域。然后创建一个NEOnDemandRuleEvaluateConnection并将其connectionRules设置为使用所有顶级域创建的NEEvaluateConnectionRule,并将其interfaceTypeMatch设置为NEOnDemandRuleInterfaceType.any。用这样创建的NETunnelProviderManager.onDemandRules设置NEOnDemandRuleEvaluateConnection。如果您如上所述创建NETunnelProviderManagerand load itsave it,则可以使用NETunnelProviderManager.isEnabledNETunnelProviderManager.isOnDemandEnabled属性打开和关闭它。

这是一个完全相同的示例类。

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,只是使用随需应变规则。

像我一样讨厌谷歌,也许用你设置的DNS ... Quad9

© www.soinside.com 2019 - 2024. All rights reserved.