ios - 如何让 AVPlayer 检索受 SSL 保护的播放列表?

标签 ios ipad ssl avplayer http-live-streaming

我们正在开发一款 HTTP 流式 iOS 应用程序,它要求我们从安全站点接收播放列表。此站点要求我们使用自签名 SSL 证书进行身份验证。

在将 NSURLConnection 与委托(delegate)一起使用以对授权质询使用react之前,我们从 .p12 文件中读取了凭据。

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    [[challenge sender] useCredential:  self.credentials forAuthenticationChallenge:challenge];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return YES;
}

通过初始连接到我们获取 .m3u8 播放列表的 URL,我们能够使用 AVPlayer 播放播放列表。问题是这种方法只在模拟器中有效。

注意:我们可以使用设备上的 NSURLConnection 下载播放列表。这一定意味着 AVPlayer 无法继续使用在此初始连接期间建立的信任。

我们也曾尝试将凭据添加到 [NSURLCredentialStorage sharedCredentialStorage],但没有成功。

下面是我们的霰弹枪方法:

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:host
                                         port:443
                                         protocol:@"https"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodClientCertificate];



[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                                    forProtectionSpace:protectionSpace];

    NSURLProtectionSpace *protectionSpace2 = [[NSURLProtectionSpace alloc]
                                         initWithHost:host
                                         port:443
                                         protocol:@"https"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodServerTrust];



[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                                    forProtectionSpace:protectionSpace2];

编辑:根据 this question : 上述方法不适用于证书。

欢迎提供任何有关它为何无法在设备上运行的提示,或其他解决方案!

最佳答案

从 iOS 6 开始,AVAssetResourceLoader 可用于检索 HTTPS 安全播放列表或 key 文件。

An AVAssetResourceLoader object mediates resource requests from an AVURLAsset object with a delegate object that you provide. When a request arrives, the resource loader asks your delegate if it is able to handle the request and reports the results back to the asset.

请在下面找到示例代码。

// AVURLAsset + Loader
AVURLAsset      *asset          = [[AVURLAsset alloc] initWithURL:url options:nil];
AVPlayerItem    *playerItem     = [AVPlayerItem playerItemWithAsset:asset];
AVAssetResourceLoader *loader   = asset.resourceLoader;
[loader setDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

// AVPlayer
AVPlayer        *avPlayer       = [AVPlayer playerWithPlayerItem:playerItem];

您将需要处理 resourceLoader:shouldWaitForLoadingOfRequestedResource:delegate 方法,该方法将在需要身份验证时调用,您可以使用 NSURLConnection 来请求安全资源。

(BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader    shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{

 //Handle NSURLConnection to the SSL secured resource here
  return YES;
}

希望这对您有所帮助!

P.S:使用 CocoaHTTPServer 的代理方法效果很好,但使用 AVAssetResourceLoader 是更优雅的解决方案。

关于ios - 如何让 AVPlayer 检索受 SSL 保护的播放列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9000614/

相关文章:

ios - 弹出窗口关闭时禁用 UIBarButtonItem

ios - 为许多图像设置动画时出现内存问题

c++ - 我如何在 QT 中将 SSL 证书添加到我的代码中

ios - libcrypto 和 libGoogleAdMobAds 中的重复符号 _MD5

iphone - 如何在 NSThread 中等待直到 iOS 中发生某些事件?

JavaScript 为计算机执行,但不为 iPad 执行

python - 通过 django 中的 ssl 证书进行用户身份验证

email - 后缀电子邮件 TLS header 但未针对 gmail 加密

ios - swift ,核心数据 : TableView not reloading data when childVC dismissed

objective-c - tabBarController 和第一个 Tab viewDidLoad