HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何让AVPlayer检索由SSL保护的播放列表?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们正在开发一个HTTP流媒体iOS应用程序,要求我们从安全站点接收播放列表.此站点要求我们使用自签名SSL证书进行身份验证.

在我们将NSURLConnection与委托一起使用以响应授权质询之前,我们从.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安全播放列表或密钥文件.

请在下面找到示例代码.

// 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保护的播放列表?全部内容,希望文章能够帮你解决ios – 如何让AVPlayer检索由SSL保护的播放列表?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: