Set useragent in WKWebview

前端 未结 6 1960
野趣味
野趣味 2021-01-31 14:25

How do I set a custom useragent string in a WKWebView? I\'m trying to embed the version of my app so that my server-side can see what features are available. I found the followi

6条回答
  •  盖世英雄少女心
    2021-01-31 14:59

    WKWebView Swift 3 example:

    let userAgentValue = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4"
    webView.customUserAgent = userAgentValue
    

    Note to those who try to do this using Storyboard or Interface Builder: Unfortunately, Xcode doesn't currently support using WKWebView in Storyboards (Xcode version 8.3.2), so you have to add the web view manually in your code.

    UIWebView Swift 3 example:

    UserDefaults.standard.register(defaults: ["UserAgent": userAgentValue])
    

提交回复
热议问题