How can I use an AVPlayer with HTTPS and self-signed server certificates?
<p>I have a server that uses self-signed SSL certificates for HTTPS. I have the self-signed root cert bundled into my app. I'm able to get <code>NSURLSession</code> to use and validate the self-signed root cert by using <code>SecTrustSetAnchorCertificates()</code> in the <code>-URLSession:didReceiveChallenge:completionHandler:</code> delegate method.</p>

<p>When I try this with <code>AVPlayer</code>, however, I get an SSL error and playback fails. This is my <code>AVAssetResourceLoader</code> delegate implementation:</p>

<pre><code>- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForResponseToAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqual:NSURLAuthenticationMethodServerTrust]) {
        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)self.secTrustCertificates);

        SecTrustResultType trustResult = kSecTrustResultInvalid;
        OSStatus status = SecTrustEvaluate(trust, &amp;trustResult);

        if (status == errSecSuccess &amp;&amp; (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:trust] forAuthenticationChallenge:challenge];
            return YES;
        } else {
            [challenge.sender cancelAuthenticationChallenge:challenge];
            return YES;
        }
    }
    return NO;
}
</code></pre>

<p>The delegate gets called, and the <code>trustResult</code> equates to <code>kSecTrustResultUnspecified</code> (which means "trusted, without explicit user override"), as expected. However, playback fails shortly after, with the following <code>AVPlayerItem.error</code>:</p>

<blockquote>
  <p>Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x16c35720 {Error Domain=NSOSStatusErrorDomain Code=-1200 "(null)"}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.}</p>
</blockquote>

<p>How can I get <code>AVPlayer</code> to accept the SSL handshake?</p>