Ios 通过MPMoviePlayer播放带有凭证的大型电影

Ios 通过MPMoviePlayer播放带有凭证的大型电影,ios,stream,xcode5,mpmovieplayercontroller,nsurlcredential,Ios,Stream,Xcode5,Mpmovieplayercontroller,Nsurlcredential,我一直在尝试从受保护的url流式传输电影。我可以下载电影然后播放,但是电影太长了,所以这很烦人 这是我的密码: -(MPMoviePlayerController *)moviePlayerController { NSURL *url = [NSURL URLWithString:@"http://ABcDE.com/secret/Movie.mov"]; _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL

我一直在尝试从受保护的url流式传输电影。我可以下载电影然后播放,但是电影太长了,所以这很烦人

这是我的密码:

-(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;
}
我试着将这个领域链接到零,但没有成功。我尝试在

   [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: credential forProtectionSpace: protectionSpace];           
那也没用

从方法-(void)movieplaybackdidfish:(NSNotification*)通知 我得到错误域=MediaPlayerDomain代码=-1013“该操作无法完成。(MediaPlayerErrorDomain错误-1013)。”

查看苹果的文档,这是一个CFNetwork错误kcfurerrorUserAuthenticationRequired=-1013


有没有办法解决这个问题?

我无法让
MPMoviePlayerController
正确地执行身份验证挑战,即使苹果的文档中有相反的说法。我提出的一个非常棘手的解决方案是使用苹果的
CustomHTTPProtocol
拦截响应并提供身份验证质询响应。我相信这个协议最初的目的是处理
UIWebViews
的身份验证

链接到
CustomHTTPProtocol

我的接口声明:

@interface SampleViewController() <CustomHTTPProtocolDelegate>
同样在我的
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];
}
-(BOOL)customHTTPProtocol:(customHTTPProtocol*)协议可以针对protectionSpace进行身份验证:(NSURLProtectionSpace*)protectionSpace
{
BOOL-canAuth=([[protectionSpace authenticationMethod]isEqual:nsurAuthenticationMethodHttpBasic]&&
[[protectionSpace领域]IseQualtString:];
返回卡纳斯;
}
-(void)customHTTPProtocol:(customHTTPProtocol*)协议didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)质询
{
NSURLCredential*newCredential=[NSURLCredential Credential WithUser:
密码:
持久性:NSURLDERSENTIALDERSENTISTEFORSESSION];
[[challenge sender]使用凭证:验证的新凭证挑战:挑战];
}
调用
start
后,所有http和https请求都会通过
CustomHTTPProtocol
模块

我没有包括
CustomHTTPProtocol
,因为苹果提供了源代码,而且非常长。我做了一些修改,使它与ARC一起工作,但基本上是相同的代码


希望这对您有效。

如果您的视频服务器需要基本身份验证,则可以轻松地将其传递到电影播放器控制器的URL,例如,您将以以下格式传递URL,而不是常规URL:

http(s)://user:password@host/path

然后将播放视频。

这看起来会有所帮助。我搞不清楚一切是怎么安排的。我可以给你的代码添加一个链接,这样我就可以看到你是怎么做的。我不能链接到我的代码,但我用我的相关代码编辑了我的帖子。对不起,我一直很忙。这对我不起作用。我将MoviePlayerController的URL更改为
http(s)://user:password@host/path