iOS: Playing video that needs authentication works

2019-04-25 02:48发布

问题:

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:

Check AuthName in your Apache config, if it's set use it in your NSURLProtectionSpace constructor as a value for realm attribute

EDIT: Sorry, didn't see your comment about FORM Authentication. hope it will help someone with BASIC Authentication



回答2:

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