美文网首页
iOS检测网络是否挂了代理(swift版)

iOS检测网络是否挂了代理(swift版)

作者: 晓晓魔导师 | 来源:发表于2019-08-21 00:45 被阅读0次
            let dic = CFNetworkCopySystemProxySettings()!.takeUnretainedValue()
            let arr = CFNetworkCopyProxiesForURL(URL(string: "https://www.baidu.com")! as CFURL, dic).takeUnretainedValue()
            
            let obj = (arr as [AnyObject])[0]
            
            let host = obj.object(forKey: kCFProxyHostNameKey) ?? "null"
            let port = obj.object(forKey: kCFProxyPortNumberKey) ?? "null"
            let type = obj.object(forKey: kCFProxyTypeKey) ?? "null"
            
            print(host)
            print(port)
            print(type)
            
            if obj.object(forKey: kCFProxyTypeKey) == kCFProxyTypeNone {
                print("没有设置代理")
                let alert = UIAlertController(title: "代理检测", message: "没有设置代理", preferredStyle: .alert)
                present(alert, animated: true, completion: nil)
            }else {
                print("设置代理了")
                let alert = UIAlertController(title: "代理检测", message: "设置代理了\n\(host)\n\(port)\n\(type)", preferredStyle: .alert)
                present(alert, animated: true, completion: nil)
            }
    

    相关文章

      网友评论

          本文标题:iOS检测网络是否挂了代理(swift版)

          本文链接:https://www.haomeiwen.com/subject/mjjvsctx.html