Change User-Agent

前端 未结 3 518
情话喂你
情话喂你 2020-12-19 19:38

How can I change in my WebView the default string of User-Agent?

@IBOutlet weak var myWbView: UIWebView!
let myURL = NSURL(string: \"http://http://web-exampl         


        
相关标签:
3条回答
  • 2020-12-19 20:01

    Swift 4.2

    You always can override global UserAgent field, it is resolve your problem:

    UserDefaults.standard.register(defaults: ["UserAgent" : "Custom UserAgent)"])
    
    0 讨论(0)
  • 2020-12-19 20:17

    If you want to set User-Agent HTTP header for your request that is going to be used for Web-view loading,

    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")
    

    If you want to set User-Agent for all requests in your app, see this question How can I set the "User-Agent" header of a UIWebView in Swift

    0 讨论(0)
  • 2020-12-19 20:17

    Actually, this is very easy. For that, you should use NSMutableURLRequest, initialize it with the NSURL, and set any user agent value using method setValue:ForHTTPHeaderField:, where field would be User-Agent, load it on the web view. That's it! Good luck!

    0 讨论(0)
提交回复
热议问题