2

Is there a way to get AVPlayer to enforce certificate pinning in iOS?

Our video loading code is basically:

let url = URL(string: "https://www.example.com/file.mp4")!
let item = AVPlayerItem(url: url)

We're able to do certificate pinning with URLSession using this delegate method: https://developer.apple.com/documentation/foundation/urlsessiondelegate/1409308-urlsession. But I haven't been able to figure out an analogous approach for AVPlayer, if there is one.

Thanks for your help!

Zach
  • 75
  • 5

1 Answers1

1

The relevant method to implement should be this one in AVAssetResourceLoaderDelegate (docs):

optional func resourceLoader(_ resourceLoader: AVAssetResourceLoader, 
     shouldWaitForResponseTo authenticationChallenge: URLAuthenticationChallenge) -> Bool

In your case, create an AVURLAsset directly, and set your delegate implementation on its resource loader. Then initialize the player item with the asset.

let url = URL(string: "https://www.example.com/file.mp4")!
let urlAsset = AVURLAsset(url: url)
urlAsset.resourceLoader.setDelegate(resourceLoaderDelegate,
                                    queue: .main)
let item = AVPlayerItem(asset: urlAsset)
Sterling Archer
  • 961
  • 8
  • 16