How can I set the "User-Agent" header of a UIWebView in Swift
Asked Answered
P

3

23

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)
    }
}
Puss answered 6/10, 2014 at 15:50 Comment(0)
P
42

This will change the agent of any call made through the 'normal' stack.

Swift 2:

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "Custom-Agent"])

Swift 3:

UserDefaults.standard.register(defaults: ["UserAgent": "custom value"])
Puss answered 6/10, 2014 at 17:8 Comment(7)
One important caveat here: this will modify the user-agent for all http requests generated by your app (at least those that are using the URL Loading System -- NSURLConnection and friends). This includes all UIWebViews in your app, not only a specific one.Luben
@FarhadMammadli registerDefaults is usually called in didFinishLaunchingWithOptions within the AppDelegateAnthropomorphic
in Swift 3, it's UserDefaults.standard.register(defaults: ["UserAgent": "whatever"])Mesonephros
why are we setting UserAgent when the HTTP header has a hyphen in it like User-Agent? Is there a typo in the answer or does UserDefaults somehow convert this to the correct header name? Edit: tried it out and looks like UserAgent is in fact correct.Stow
Is there a way to exclude the custom user-agent for certain hosts or use it explicitly for a certain host? My problem is, that when using it via registerDefaults, the facebook like-button no longer works as it seems to check for the User-Agent. As soon as I comment out the registerDefaults line faceboooks works. My first attempt was to check the request in webView(UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebViewNavigationType) but it seems that the User-Agent will always set back to the default one.Whoop
Be careful with that one... I just found this line of code to be responsible for preventing Google Mobile Ads to load. You should rather extend the existing user agent as described in @PassKit's answer.Esquire
Can someone confirm that this is still working for iOS >= 15. From what I see, the User-Agent looks as follow, and cannot globally be changed for the entire app. <AppName>/<AppVersion> CFNetwork/<CFNetworkVersion> Darwin/<DarwinVersion>Genista
C
29

If you want to append to the existing user agent string (useful for helping dynamic webpages that adapt to the user agent), then you can use the following.

let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Custom-Agent"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])
Charitycharivari answered 6/12, 2014 at 11:7 Comment(1)
you should not force-unwrap the user agent, using if let would be better. Also, creating a UI element to retrieve a property is also not the best approach and will lead to problems (for example when Unit testing)Anthropomorphic
N
0

To switch between fake user agent and original one, you can use my function:

Swift 4:

var originalUserAgent:String? = nil
let fakeUserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

func setUserAgent(fake: Bool) {
    if originalUserAgent == nil {
        originalUserAgent = UIWebView().stringByEvaluatingJavaScript(from: "navigator.userAgent")
    }

    guard let originalUserAgent = originalUserAgent else {
        return
    }

    if (fake) {
        UserDefaults.standard.register(defaults: ["UserAgent": fakeUserAgent])
    } else {
        UserDefaults.standard.register(defaults: ["UserAgent": originalUserAgent])
    }
}

// then use
setUserAgent(fake: false)
Neilneila answered 8/5, 2018 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.