gpt4 book ai didi

swift - WKWebView:如何禁用防止跨站点跟踪?

转载 作者:行者123 更新时间:2023-12-05 06:18:29 36 4
gpt4 key购买 nike

我的应用程序有 1 个 ViewController,它加载一个使用 Auth0 处理登录的网络应用程序 url。在 iOS 13.3 中,webView 已停止在登录后加载其他页面。

但是,当我尝试在 Mac 上使用 Safari 时,如果我禁用“防止跨站点跟踪”,则 Web 应用程序可以正常工作。我如何在 WKWebView 中执行此操作?

preferences

class MainViewController: UIViewController {

private var webView: WKWebView!
private var webViewConfiguration: WKWebViewConfiguration = {
let source: String = "var meta = document.createElement('meta');" +
"meta.name = 'viewport';" +
"meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
"var head = document.getElementsByTagName('head')[0];" + "head.appendChild(meta);";
let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let userContentController: WKUserContentController = WKUserContentController()
let conf = WKWebViewConfiguration()
conf.userContentController = userContentController
userContentController.addUserScript(script)
return conf
}()

override var prefersStatusBarHidden: Bool {
return true
}

override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

private func setupView() {
webView = WKWebView(frame: self.view.frame, configuration: webViewConfiguration)
webView.scrollView.delegate = self
webView.navigationDelegate = self

title = "My App"
view.backgroundColor = UIColor.white
view.addSubview(webView)
view.addConstraintsWithFormat("H:|[v0]|", views: webView)
view.addConstraintsWithFormat("V:|[v0]|", views: webView)

// Adding deviceId and deviceName for analytics
let endpointString = AppSettings.appUrl + "/?deviceId=\(deviceId)&deviceName=\(deviceName)"
let escapedUrlString = endpointString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""

if let url = URL(string: escapedUrlString) {
Logger.log(message: "Loading with url - \(escapedUrlString)", event: .info)
let request = URLRequest(url: url)
webView.load(request)
}
}
}

extension MainViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
shouldShowNativeError = false
if let url = webView.url?.absoluteString {
Logger.log(message: "didFinish loading url: \(url)", event: .info)
}
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
let exceptions = SecTrustCopyExceptions(serverTrust)
SecTrustSetExceptions(serverTrust, exceptions)
let credential = URLCredential(trust: serverTrust)
completionHandler(.useCredential, credential)
} else {
completionHandler(.useCredential, nil)
}
}

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url,
let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let scheme = components.scheme {

if scheme == "blob" {
self.blobItemUrl = url
navigationController?.setNavigationBarHidden(false, animated: true)
navigationItem.leftBarButtonItem = backBarButtonItem
navigationItem.rightBarButtonItem = shareBarButtonItem
}
}
decisionHandler(.allow)
}
}

最佳答案

尝试

self.webView.configuration.processPool.perform(Selector(("_setCookieAcceptPolicy:")), with: HTTPCookie.AcceptPolicy.always)

放在“setupView()”中,在“webView.navigationDelegate = self”之后

编辑:显然,如果你想在 App Store 中发布你的应用程序,你不能使用它

关于swift - WKWebView:如何禁用防止跨站点跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61247319/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com