在 iOS WKWebView 中保留“Referer”标头?
Posted
技术标签:
【中文标题】在 iOS WKWebView 中保留“Referer”标头?【英文标题】:Persist “Referer” header in iOS WKWebView? 【发布时间】:2021-07-16 06:32:24 【问题描述】:我在 WKWebView 中传递自定义标题。但是在重定向标头上会更新为新标头。 例如,我在浏览器中输入了“MyName”,现在打印语句Referer 已更改“Referer”,它将打开搜索结果:“https://www.google.com/” 如何在重定向中保留自定义标头。
在 WKWebView, decidePolicyFor navigationAction: WKNavigationAction
中,函数头在重定向时得到更新。
navigationAction.request.setValue("/App/app/ios", forHTTPHeaderField: "Referer")
这不行
import WebKit
class WKWEBViewController: UIViewController, WKNavigationDelegate
private var wkWebView: WKWebView!
var loadURLString = "https://www.google.com"
var customHeaders:[String:String] = ["Referer":"/AppleApp/app/iOS"]
override func viewDidLoad()
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(true)
loadURL()
private func loadURL()
let config = WKWebViewConfiguration()
wkWebView = WKWebView(frame: UIScreen.main.bounds, configuration: config)
wkWebView.navigationDelegate = self
wkWebView.backgroundColor = .white
wkWebView.scrollView.backgroundColor = .white
view.backgroundColor = .white
view.addSubview(wkWebView)
if let pdfurl = URL(string: loadURLString)
var urlRequest = URLRequest(url: pdfurl)
if customHeaders.count != 0
for obj in customHeaders
urlRequest.addValue(obj.value, forHTTPHeaderField: obj.key)
wkWebView.load(urlRequest)
extension WKWEBViewController
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
let request = navigationAction.request
print("URl--->", request.url)
if let headers = request.allHTTPHeaderFields
print("Headers: \(headers)")
else
print("Nope, sir")
decisionHandler(.allow)
【问题讨论】:
那些客户标题是什么? 之前var customHeaders:[String:String] = ["Referer":"/AppleApp/app/iOS"]
之后"Referer": "https://www.google.com/"
@ElTomato
【参考方案1】:
喜欢下面的?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate
// MARK: - IBOutlet
@IBOutlet weak var webView: WKWebView!
// MARK: - Life cycle
override func viewDidLoad()
super.viewDidLoad()
webView.navigationDelegate = self
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
let urlStr = "https://www.google.com/"
if let url = URL(string: urlStr)
let request = URLRequest(url: url)
webView.load(request)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
var request = navigationAction.request
if let host = navigationAction.request.url?.host
if host == "www.google.com"
request.setValue("https://www.google.com/", forHTTPHeaderField: "Referer")
if let headers = request.allHTTPHeaderFields
print("Google Headers: \(headers)")
decisionHandler(.allow)
以下是结果
Google 标头:[“Referer”:“https://www.google.com/”、“User-Agent”: “Mozilla/5.0(iPhone;CPU iPhone OS 14_4 像 Mac OS X) AppleWebKit/605.1.15(Khtml,如 Gecko)”、“接受”: "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8"]
【讨论】:
但是您没有将标题分配给navigationAction
& in WKWebView
我明白了。我不知道你能不能做到。
我试过navigationAction.request.setValue("/App/app/iOS", forHTTPHeaderField: "Referer")
不行以上是关于在 iOS WKWebView 中保留“Referer”标头?的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Xamarin IOS 中捕获 WkWebView 的屏幕