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

ios – 如何让AVPlayer检索由SSL保护的播放列表?,第1张

概述我们正在开发一个HTTP流媒体iOS应用程序,要求我们从安全站点接收播放列表.此站点要求我们使用自签名SSL证书进行身份验证. 在我们将NSURLConnection与委托一起使用以响应授权质询之前,我们从.p12文件中读取凭据. - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:( 我们正在开发一个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],但没有任何运气.

下面是我们的霰dq方法:

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安全播放列表或密钥文件.

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 + LoaderAVURLAsset      *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)];// AVPlayerAVPlayer        *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保护的播放列表?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1086026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存