2012-07-02 75 views
4

我在從具有基本http認證的URL播放電影時遇到問題。從需要訪問憑據的URL播放電影的MediaPlayer.framework(MPMoviePlayerController)

這裏是代碼:

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)。」,錯誤日誌中NULL,就像ACCESSLOG了。

我使用AuthType Basic的apache服務器,憑據是正確的,在Web瀏覽器上測試它們。如果禁用了認證,則播放沒有問題。

請幫忙,我找不到錯在哪裏。

+1

這是完全有可能的,電影播放器​​不諮詢URL憑證存儲,因爲它沒有在內部使用URL加載系統:( –

+0

那麼有沒有其他的方式來從需要認證的網址播放媒體? – Moze

+0

我很想我知道你是否想出了這個問題,我同意Mike Abdullah關於不使用你設置的憑證,我看到了同樣的問題,我也有問題,一旦我成功設置證書,我不能刪除它們。我找到的解決方案都沒有幫助。 – SteveB

回答

0

我無法得到MPMoviePlayerController正確地執行身份驗證挑戰,甚至認爲蘋果文檔中有其他說法。我提出的非常冒險的解決方案是使用Apple的CustomHTTPProtocol截獲響應並提供身份驗證質詢響應。我相信這個協議的最初目的是爲了處理UIWebViews的認證。

鏈接CustomHTTPProtocolhttps://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/Read_Me_About_CustomHTTPProtocol_txt.html

我的接口聲明:

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,我有一對夫婦的委託方法:MPMoviePlayerControllerSampleViewController

@interface SampleViewController() <CustomHTTPProtocolDelegate> 

實例化。對於基本身份驗證,這是很簡單的:

- (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,因爲蘋果提供源,這真的很長。我做了一些改變,使它與ARC一起工作,但它大部分是相同的代碼。

希望這對你的作品。

相關問題