我一直在尝试从受保护的网址流式传输电影。我可以下载电影然后播放,但是电影太长了,这很烦人。
这是我的代码:
-(MPMoviePlayerController *)moviePlayerController
{
NSURL *url = [NSURL URLWithString:@"http://ABcDE.com/secret/Movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser: @"user"
password: @"password"
persistence: NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: [url host]
port: 80
protocol: [url scheme]
realm: [url host]
authenticationMethod: NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential: credential
forProtectionSpace: protectionSpace];
_moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
_moviePlayer.allowsAirPlay = YES;
_moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
return _moviePlayer;
}
我尝试将领域链接到无用的nil。我尝试在移动initWitcontnetURL之后
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: credential forProtectionSpace: protectionSpace];
那也不起作用。
从方法-(void)moviePlayBackDidFinish:(NSNotification *)notification
我收到错误错误域= MediaPlayerDomain代码= -1013“操作无法完成。(MediaPlayerErrorDomain错误-1013。)”
查看苹果文档,这是一个CFNetwork错误kCFURLErrorUserAuthenticationRequired = -1013
任何想法如何解决这个问题?
最佳答案
我无法获得MPMoviePlayerController
来正确地执行身份验证挑战,甚至以为Apple文档另有说明。我想到的一个非常棘手的解决方案是使用Apple的CustomHTTPProtocol
拦截响应并提供身份验证质询响应。我相信此协议的最初目的是处理UIWebViews
的身份验证。
链接到CustomHTTPProtocol
:
https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/Read_Me_About_CustomHTTPProtocol_txt.html
我的接口声明:
@interface SampleViewController() <CustomHTTPProtocolDelegate>
在我的
MPMoviePlayerController
中实例化SampleViewController
:NSString *fullURLString = @"http://www.samplesite.com/samplemovie.mp4";
NSURL *fullURL = [NSURL URLWithString:fullURLString];
[CustomHTTPProtocol setDelegate:self];
[CustomHTTPProtocol start];
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:fullURL.host
port:80
protocol:fullURL.scheme
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fullURL];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO];
[self.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.moviePlayer.view setFrame:self.sampleView.bounds];
[self.moviePlayer.backgroundView setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
[self.moviePlayer.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.sampleView addSubview:self.moviePlayer.view];
同样在
SampleViewController
中,我有几个委托方法。对于基本身份验证,这非常简单:- (BOOL)customHTTPProtocol:(CustomHTTPProtocol *)protocol canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
BOOL canAuth = ([[protectionSpace authenticationMethod] isEqual:NSURLAuthenticationMethodHTTPBasic] &&
[[protectionSpace realm] isEqualToString:<your realm>]);
return canAuth;
}
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:<username>
password:<password>
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
调用
start
后,所有http和https请求都将通过CustomHTTPProtocol
模块我没有包括
CustomHTTPProtocol
,因为Apple提供了源代码,而且确实很长。我进行了一些更改以使其能够与ARC一起使用,但是它几乎是相同的代码。希望这对您有用。