quantum
quantum

Reputation: 1420

UIWebView modal YouTube player "Done" button action

In my iPhone app, I'm presenting a modal view controller from a home screen with a UIWebView that displays an "inline" embedded YouTube video using this:

UIWebView *youTubeWV = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 220)];
        [youTubeWV loadRequest:[NSURLRequest requestWithURL:sourceURL]];
        //NSString *youTubeVideoHTML = [NSString stringWithFormat:@"<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=CadgUJRZfEE\" type=\"application/x-shockwave-flash\" width=\"320\" height=\"220\"></embed>"];
        NSString *youTubeVideoHTML =@"<html><head>"
        "<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 220\"/></head>"
        "<body style=\"background:#FFFFF;margin-top:0px;margin-left:0px\">"
        "<div><object width=\"320\" height=\"220\">"
        "<param name=\"wmode\" value=\"transparent\"></param>"
        "<embed src=\"http://www.youtube.com/v/W-nzUoaI2Ss?f=user_favorites&app=youtube_gdata\""
        "type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"220\"></embed>"
        "</object></div></body></html>";
        [youTubeWV loadHTMLString:youTubeVideoHTML baseURL:nil];
        [self.view addSubview:youTubeWV];

The video is displayed without problem in the modal view "quicktime player" that shows. However, when I tap "Done" to close the second modal, I get kicked back to the very first screen, bypassing my first modal view. And in my home screen, now all the buttons don't work. Strange!

UPDATE: I removed the modal transition from my home screen and made it a "pushViewController" instead, and now everything functions properly. So it's an issue of the YouTube player dismiss 2 modals at the same time. How can this be fixed?

Any ideas?

Upvotes: 9

Views: 4210

Answers (3)

SundialSoft
SundialSoft

Reputation: 136

The answer re: "I solved this setting my Modal View as my rootViewController after presenting it." is the correct approach. If you call youtube from a modally presented view controller it will return to the original controller which presented the modal one. If you then try to re-present the correct controller using flags it gets really messy and there can be timing issues. Izaakcito's method works properly.

Upvotes: 0

IsaacCisneros
IsaacCisneros

Reputation: 1953

I'm not sure but it seems that after the "done" button is pressed and the Player is closed the control returns to the root view controller, in your case that's your first screen.

For exaple in my application I have an UITabBarController as my rootViewController, in my AppDelegate I have something like this:

self.window.rootViewController = self.myTabBarController;

So that's the reason why my tabBarController (my first screen) always was presented after pressing the "done" button.

I solved this setting my Modal View as my rootViewController after presenting it.

Try this after presenting your modal view:

MyAppDelegate *appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[self presentModalViewController: myModalViewController animated: YES];
[self.appDelegate.window setRootViewController: myModalViewController];

NOTE: After dismissing the modal view you must restore the root view controller doing

In my case:

self.appDelegate.rootViewController = self.appDelegate.myTabBarController;

Hope it helps

Upvotes: 12

Suresh Varma
Suresh Varma

Reputation: 9740

Not an exact solution but a kind of patch can may be help you out.

Take a global flag (may be in ApplicationDelegate) and set it to true when ever you tap on Done button on the youtube page. And in the very first controller viewWillAppear check if the flag is TRUE then present the second controller and turn the flag FALSE again.

Hope it helps.

Upvotes: 0

Related Questions