gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:07:01 25 4
gpt4 key购买 nike

我们正在开发一款 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com