ios - 更改用户代理

标签 ios swift user-agent nsurlrequest

如何在我的 WebView 中更改 User-Agent 的默认字符串?

@IBOutlet weak var myWbView: UIWebView!
let myURL = NSURL(string: "http://http://web-example")
let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!)
myWbView.loadRequest(myURLRequest)

最佳答案

如果您想为将要用于 Web View 加载的请求设置 User-Agent HTTP header ,

let userAgent = "Custom User Agent";
let myURL = NSURL(string: "http://http://web-example")
let myURLRequest:NSURLRequest = NSMutableURLRequest(URL: myURL!)
myWbView.loadRequest(myURLRequest)    
myURLRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")

如果您想为应用中的所有请求设置 User-Agent,请参阅此问题 How can I set the "User-Agent" header of a UIWebView in Swift

关于ios - 更改用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889970/

相关文章:

java - 在 GWT 中基于浏览器用户代理更改 UiTemplate

ios - 如何在 Apple 开发者帐户中以开发者角色生成 IPA 文件

ios - 添加和删​​除某个 UITableViewCell 下面的 UITableViewCell

ios - Apple Watch 背景模式?

ios - 如何在 UIViewcontroller 中访问已安装的 pod 库宏字符串?

database - 存储用户代理有什么意义?

iphone - iPhone 应用程序中的 Gmail 身份验证

ios - iOS 10 中新键盘点击的系统声音 ID 是什么?

ios - 在 iOS : Lines and Rectangles 中创建 PDF

perl - 从 Mojolicious 用户代理响应中提取 cookie