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

1

9

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?

Devoirs answered 20/3, 2012 at 16:18 Comment(5)
+1 for good question - curious on the answers. Generally speaking, MPMoviePlayerController (just as AVPlayer which the former relies on) do not seem to make much use of such shared, high-level networking functionalities.Where
I am using one of my iOS dev support tickets and sent this question off to Apple. Will see what they say..Devoirs
Yeah, they said that it wouldn't work because I have a normal HTML form login, rather than HTTP auth.Devoirs
Are you able to fix this problem Nic ? I'm facing the same problem now.Varden
Nope. Even submitted a support ticket to Apple. They said it can't be done.Devoirs
A
0

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://mcmap.net/q/1321207/-possible-to-play-video-using-a-subclass-of-nsurlprotocol-using-either-mpmoviecontroller-or-avfoundation

Arrest answered 24/4, 2014 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.