iOS: Playing video that needs authentication works in QuickLook but not in MPMoviePlayerViewController

自古美人都是妖i 提交于 2019-12-22 04:35:53

问题


I login to my server using a SOAP web service. Once logged in, many of the files that I am viewing are only available to the logged in user, so iOS must create a session in NSURL or something.

When trying to preview a video file using MPMoviePlayerViewController it will not work, it just loads up the viewController, then dismisses it.

If I use QuickLook it does work, probably because I download the video locally first, then view it.

But, I don't want to do it this way, I want to stream the video using MPMoviePlayerViewController because I don't want the user to have to download an entire video file. I have seen posts about using NSURLCredential but that doesn't seem to work for me. I used (added my own personal info obviously):

/**
 * Play media session
 *
 * @version $Revision: 0.1
 */
- (void)playMediaWithURL:(NSString *)mediaURL {

    // Authenticate
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"myusername"
                                                             password:@"mypassword"
                                                          persistence:NSURLCredentialPersistenceForSession];

    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                             initWithHost:@"mysite.com"
                                             port:80
                                             protocol:@"http"
                                             realm:nil
                                             authenticationMethod:NSURLAuthenticationMethodDefault];

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

    // The movie player
    NSURL *movieURL = [NSURL URLWithString:[mediaURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    MPMoviePlayerViewController *tempPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];

    // Add observer
    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector:@selector(moviePlayBackDidFinish:) 
         name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    // Properties
    tempPlayer.moviePlayer.allowsAirPlay = YES;
    tempPlayer.moviePlayer.shouldAutoplay = YES;
    tempPlayer.moviePlayer.useApplicationAudioSession = NO;
    [self presentMoviePlayerViewControllerAnimated:tempPlayer];
    [tempPlayer.moviePlayer play];

}//end

Since this video is only viewable by a logged in user, if the video URL is accessed by a public user, they are presented with an HTML form to login. Does NSURLCredential not work in this case?

Why do all calls to NSURLConnection work, using my logged in credentials (such as downloading the video), but MPMoviePlayerViewController doesn't seem to use those same credentials, and refuses to play the video (probably because it gets the login page)?

Is there a solution to this?


回答1:


Recently, I had a similar problem not being able to pass cookies to MPMoviePlayerController. I found from stack overflow that the solution is to use NSURLProtocol. Still, it was painful figuring out how to do it, so I thought I'd save people some time by sharing the coded solution: https://stackoverflow.com/a/23261001/3547099



来源:https://stackoverflow.com/questions/9790697/ios-playing-video-that-needs-authentication-works-in-quicklook-but-not-in-mpmov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!