我们正在开发一个超文本传输协议流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:上述方法不适用于证书。
任何提示,为什么它不能在设备上工作,或替代解决方案是受欢迎的!
发布于 2013-10-26 15:21:45
从HTTPS6开始,AVAssetResourceLoader可用于检索iOS安全播放列表或密钥文件。
AVAssetResourceLoader对象使用您提供的委托对象协调来自AVURLAsset对象的资源请求。当请求到达时,资源加载器会询问您的委托是否能够处理该请求,并将结果报告回资产。
请找到下面的示例代码。
// 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;
}希望这能有所帮助!
附言:使用CocoaHTTPServer的代理方法工作得很好,但使用AVAssetResourceLoader是一个更优雅的解决方案。
发布于 2012-02-02 17:03:25
似乎在苹果让我们控制AVPlayer使用什么NSURLConnections之前,唯一的答案似乎是实现一个HTTP回送服务器。
引用回答我们支持问题的苹果代表的话:
的另一个选择是实现一个环回HTTP服务器,并在那里指向客户端对象。客户端可以使用HTTP (因为它们的请求永远不会离开设备),而回送HTTP服务器本身可以使用HTTPS连接到源服务器。同样,这为您提供了对底层NSURLConnections的访问权限,允许您执行自定义服务器信任评估。
在UIWebView中使用此技术将很棘手,除非您完全控制源服务器上的内容。如果源站可以返回任意内容,您必须卑微地通过返回的HTTP并重写所有链接,这并不是很有趣。类似的限制适用于MPMoviePlayerController/AVPlayer,但在这种情况下,更常见的是控制所有内容,从而能够避免非相对链接。
编辑:我设法使用CocoaHTTPServer中的HTTPResponse和HTTPConnection类的自定义实现实现了一个环回服务器
我不能透露消息来源,但我将NSURLConnection与AsyncHTTPResponse和DataHTTPResponse演示响应结合使用。
编辑:记得设置myHttpServerObject.interface = @"loopback";
编辑:警告!这种方法似乎不适用于airplay,因为airplay设备将要求127.1.1.1提供加密密钥。正确的方法似乎定义在这里:https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/AirPlayGuide/EncryptionandAuthentication/EncryptionandAuthentication.html#//apple_ref/doc/uid/TP40011045-CH5-SW1
“使用应用程序定义的.m3u8方案指定URL文件中的密钥。”
编辑:苹果电视和iOS的更新已经解决了上面的编辑中提到的问题!
https://stackoverflow.com/questions/9000614
复制相似问题