我在从具有基本http身份验证的URL播放电影时遇到问题。

这是代码:

NSURLCredential *credential = [[NSURLCredential alloc]
                               initWithUser:@"user"
                               password:@"password"
                               persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                             initWithHost:@"moze.dyndns.org"
                                             port:80
                                             protocol:@"http"
                                             realm:nil
                                             authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];


NSURL *videoURL = [NSURL URLWithString:@"http://moze.dyndns.org/test/test.mov"];
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayerController.view];
MPMoviePlayerController *mp = [moviePlayerController moviePlayer];
mp.controlStyle = MPMovieControlStyleDefault;
[mp prepareToPlay];
[[moviePlayerController moviePlayer] play];

我收到错误消息“操作无法完成。(MediaPlayerErrorDomain错误-1013。)”,errorLog in NULL,就像accessLog一样。

我正在使用具有AuthType Basic的apache服务器,凭据正确,已在Web浏览器上对其进行了测试。如果禁用身份验证,则播放不会有问题。

请帮助,我找不到问题所在。

最佳答案

我无法获得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:@"your-realm"
                                        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一起使用,但是它几乎是相同的代码。

希望这对您有用。

09-07 13:47