Can you set a header User-Agent in Swift

So we have a podcast app, that we have developed and I can't find any documentation anywhere about setting a User-Agent when fetching external data from a URL

User-Agent

Is there an official Swift documentation about how to do this?

You can set HTTP headers in URLRequest:

https://developer.apple.com/documentation/foundation/urlrequest

let url = URL(string: "https://....")!
var request = URLRequest(url: url)
request.setValue("value", forHTTPHeaderField: "User-Agent")

and you can pass the request to URLSession just like URL.

1 Like