Anton
Anton

Reputation: 947

Setting User Agent for AVPlayer

could you please help with setting User Agent for AVPlayer?

I have a following code:

    let url = NSURL(string:"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")

    var headers: [String:String] = ["User-Agent": "myagent"]
    let asset: AVURLAsset = AVURLAsset.URLAssetWithURL(url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
    let playerItem = AVPlayerItem(asset)
    player.replaceCurrentItemWithPlayerItem(playerItem)
    player.play()

It doesn't work. How I can set User Agent in a right way in Swift?

Upvotes: 6

Views: 6325

Answers (2)

Jeff C.
Jeff C.

Reputation: 2135

Apple has provided an officially-supported way of doing this as of iOS 16 (as well as macOS 13, iPadOS 16, tvOS 16, and watchOS 9) using AVURLAssetHTTPUserAgentKey.

You can specify it as an option when creating an AVURLAsset, like this:

let asset:AVURLAsset?
if #available(iOS 16.0, *) {
    asset           = AVURLAsset(url: url, options: [AVURLAssetHTTPUserAgentKey: "MyCustomUserAgent"])
} else {
    asset           = AVURLAsset(url: url)
}

Details can be found here: https://developer.apple.com/documentation/avfoundation/avurlassethttpuseragentkey

Upvotes: 0

Anton
Anton

Reputation: 947

it is really very simple in Swift 2.3

let headerFields: [String:String] = ["User-Agent":"myua"]
let asset: AVURLAsset = AVURLAsset(URL:url!, options: ["AVURLAssetHTTPHeaderFieldsKey": headerFields])

Upvotes: 11

Related Questions