JDoe
JDoe

Reputation: 525

How can I get the “User-Agent” of a UIWebView in Swift3

I need to modify the useragent for some requests, and therefore I would either need to get it first, or append something to it. What's the best way of doing this? Do you have to use a webview to do it, or are there other ways?

what i trying :

var  userAgent = ""
    if UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent") != nil{
        userAgent  =  userAgent  + " Custom-Agent"
    }
    print(userAgent)
    UserDefaults.standard.register(defaults: ["UserAgent" : userAgent])

Upvotes: 3

Views: 4919

Answers (1)

Nazmul Hasan
Nazmul Hasan

Reputation: 10590

get user agent

var webView = UIWebView(frame: CGRect.zero)
var secretAgent: String? = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")

Change User-Agent

Upvotes: 6

Related Questions