我正在将 Xcode 6 用于带有 Swift 的 iOS 应用程序.我有一个带有嵌入式 UIWebView 的简单 ViewController.您可以在下面找到代码.现在我想更改 User-Agent
HTTP 标头.我尝试使用 NSURLRequest
的 setValue
方法,但它不起作用(请参阅未注释的行).有人知道怎么做吗?
I am using Xcode 6 for an iOS App with Swift. I have got a simple ViewController with an embedded UIWebView. You can find the code below. Now I want to change the User-Agent
HTTP header. I tried to use the setValue
method of NSURLRequest
but it didn't work (see uncommented line). Does anyone know how to do that?
import UIKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidAppear(animated: Bool) {
var url = NSURL(string: "https://www.samplepage.com")
var request = NSMutableURLRequest(URL: url)
// request.setValue("Custom-Agent", forHTTPHeaderField: "User-Agent")
webView.loadRequest(request)
}
}
这将更改通过正常"堆栈进行的任何调用的代理.
This will change the agent of any call made through the 'normal' stack.
斯威夫特 2:
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "Custom-Agent"])
斯威夫特 3:
UserDefaults.standard.register(defaults: ["UserAgent": "custom value"])
这篇关于如何设置“用户代理"?Swift 中 UIWebView 的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!