Youtube in iOS5 - done button Tapped - objective-c

In iOS5 i want to load Youtube movies, and i done it with:
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
}
calling by:
[self embedYouTube:#"http://www.youtube.com/v/PqtUSSdf8b4" frame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
And i have standard Youtube frame with "done" button. When i hit done button i'm redirect to my root VC. why? how can i get to my VC (the one when i'm calling it) ?
And more important - how to catch "done button" event ?
EDIT:
I have MainVC and DetailVC. MainVC calls DetailVC as a ModalViewController. From there I call this embedYouTube method. When i'm hitting done on this screen:
i'm returning to MainVC not DetailVC. Why? How to catch this event?
EDIT:
And iOS 6 displays only black background - nothing. Why?

The fullscreen video player will dismiss when you tap the done button, this is encapsulated into that full screen player which the system automatically presents as part of this youtube/flash safari embed trick. You shouldn't try to do anything when this is used.
About iOS 6 - iOS 6 no longer supports this way of embedding YouTube videos in your application (source)
As of iOS 6, embedded YouTube URLs in the form of
http://www.youtube.com/watch?v=oHg5SJYRHA0 will no longer work. These
URLs are for viewing the video on the YouTube site, not for embedding
in web pages. Instead, the format that should be used is described
here: https://developers.google.com/youtube/player_parameters.

The behavior of "DONE" button by default is that it dismisses all the modal view controllers, as you are presenting the DetailVC modally, so its also dismissed, you need to change the presenting method of DetailVC. Try simply by pushViewController.

Related

UIWebView breaking constraint when playing a video in fullscreen (Xcode 6)

I'm updating an iOS app for an YouTube channel. In the previous version (iOS 7 using Xcode 5), I used to embed youtube iFrame in a UIWebView and everything would work just fine.
However, in iOS 8 using Xcode 6, every time I play a Youtube video and it goes full screen, my UIWebView constraints break and UIWebView gets relocated in my Controller (usually goes 10 points up in the screen).
I've tried to change the constraints but it seems that doesn't matter what constraint I set up, it will eventually break when the player goes full screen.
Here's the html code used to embed YouTube iframe:
- (void)embedYouTube:(NSString *)urlString
{
NSString *embedHTML =[NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: blue;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"140\" width=\"325\" src=\"http://www.youtube.com/embed/%#\"></iframe>\
</body></html>",urlString];
[self.webView loadHTMLString:embedHTML baseURL:nil];
}
Since the log is huge, I've pasted it in pastebin: http://pastebin.com/z4zyq7Hs
Thank you :)
I had same problem. But, I do not experience relocation problem though (using Xcode 6.1), only bunch of constraint breaking logs.
What I tested that still cannot work:
- remove constraint and setTranslatesAutoresizingMaskIntoConstraints to YES for all the webview and its subview
- swap UIWebview with WKWebview, the newer and faster UIWebview
I also test playing Dailymotion video and it also has same problem, so I suspect this is more the problem on MediaPlayer views of UIWebview.
try like this
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
I'm about to leave work so I'll have to type this answer quickly,
one way I have found of fixing this is refreshing the cache of the page being viewed, I don't have a solid fix for this but I came up with a temporary one last week, this can be done by creating a link to a css file in the header of your html
<link id="refreshCache" rel="stylesheet" type="text/css" href="refresh.css">
it's important that you also create a refresh.css file, you are however aloud to leave it blank,
now using javascript or jQuery if you already have it linked up, use the
.remove
http://api.jquery.com/remove/
on the id refreshCache, on page load, removing the link in the header to the empty file when the page loads...
this will refresh your browsers cache, hope this helps!

How to change WebView's webpage position? OSX

Let's say I have a WebView that loads up twitter.com.
Here's what appears in my WebView:
And here's what I want to show:
How can I change the default Upper Left Corner position of website on the WebView to display another position of the same website?
I get it to move using this (you need to set the frameLoadDelegate):
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame == self.myWebview.mainFrame)
{
DOMDocument *mainDoc = [self.myWebview.mainFrame DOMDocument];
DOMNodeList *list = [mainDoc getElementsByClassName:#"front-signin js-front-signin"];
DOMNode *loginElement = [list item:0];
NSRect myRect = loginElement.boundingBox;
[self.myWebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(%d,%d);", (int)myRect.origin.x, (int)myRect.origin.y]];
}
}
"front-signin js-front-signin" being the name of the class used in the source html of www.twitter.com.
So it is failure prone to rely on that which could change at any time.
There is also in this case the header that doesn't move. So either account for it and display it, or move your view up the size of this header and hide it.
You need to adjust the scrollTo call to center it in your view.
And you might want to consider fading the view once it's loaded, cause the scroll is kind of jerky, and not very osx like.
But apart from this, it could work ;-)
You could execute javascript in the UIWebView:
[webView stringByEvaluatingJavaScriptFromString:#"window.pageYOffset=200;"];
This should scroll the window down 200 pixels.
You can use the following on OS X:
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(%f, %f)", xPos, yPos]];
You can also play with the webview's scrollView contentOffset.
UIWebView * webView = [[UIwebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.twitter.com"]]];
[webView.scrollView setContentOffset:CGPointMake(50, 60)];
[webView release];

Youtube embedded in UIWebView causes crash on iPad when entering full screen

I'm trying to embed a Youtube video using a mixture of this technique, and this Youtube SDK blog post in a universal app. The iPhone version, using the same code, works fine.
On the iPad the video does embed, and it plays fine in it's embedded form, but as soon as you tap the full screen button the app crashes (buttons do not respond, the device does not rotate). The music from the Youtube video keeps playing.
There is no error message logged but the app does register as 'Paused' or hung in xCode. Every time it crashes com.apple.libdispatch-manager is on thread 2. Ask me questions and I'll give you more information about the error, but I'm not sure where to start.
I have tried:
changing the size of the UIWebView frame
the UIWebView is in a UIScrollView, but if I take it out of the scrollview and add it to the view the problem is identical.
changing the video
changing the html that I use in the UIWebView from this to this, with no result
changing the format of the youtube link from ?v=uniqueID to /v/uniqueID
checking the presenting view is the rootviewcontroller (it is, but the video is embedded in a modal, which is not the rootviewcontroller).
I am building for iOS 5.1, this doesn't happen if running on iOS6.
The View that the video is embedded in is modal, both on the phone and the iPad. There's no hackery or unusual things happening in the app.
There seems to be talk of Evernote's app having a similar problem, but I don't know if it is related or not.
For your reference, here is the YouTubeView subclass (which subclasses UIWebView):
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[YouTubeView alloc] initWithFrame:frame];
// HTML to embed YouTube video
// NSString *youTubeVideoHTML = #"<html><head>
// <body style=\"margin:0\">
// <embed id=\"yt\" src=\"%#\"
// type=\"application/x-shockwave-flash\"
// width=\"%0.0f\" height=\"%0.0f\">
// </embed>
// </body>
// </html>";
NSString *youTubeVideoHTML = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = %0.0f\"/></head><body style=\"background:#FFF;margin-top:0px;margin-left:0px\"><div><object width=\"%0.0f\" height=\"%0.0f\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
// Populate HTML with the URL and requested frame size
// NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", youTubeVideoHTML);
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, frame.size.width, frame.size.width, frame.size.height, urlString, urlString, frame.size.width, frame.size.height];
NSLog(#"html:\n %#", html);
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
Modal view on iOS 5.0 and iOS 5.1 is the problem that causes crash on full screen video, AFAIK. They just changed hierarchy of views in that version of iOS (parentViewController and presentingViewController) and that is the aftermath. I asked about it long time ago here and one more same question is here and still no one knows what to do.
First of all, they fixed it in 6.0, I guess, that's good.
For 5.1 we changed design a little and avoided modal view. Do it, if it is possible in your situation.

MPMoviePlayerViewController customization

I'm using MPMoviePlayerViewController - with the player controls set to: MPMovieControlStyleFullscreen
I'm having a problem with some of buttons that are in MPMovieControlStyleFullscreen: forward, reverse, and fullscreen (the one with the arrows pointing at eachother).
I would like to either remove the forward, reverse, and fullscreen buttons or control what they do when the user taps them.
Thank you!
There isn't a way to customize the MPMovieControlStyle values provided by Apple. What you need to do is is turn off the Apple controls (MPMovieControlStyleNone) and then create your own custom controls. Apple is fine with you putting your own UIViews in to the hierarchy here, so you can get started with something like this:
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: YOUR_URL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
UIView *movieView = moviePlayer.view;
[movieView addSubview: _movieControlsView];
[movieView bringSubviewToFront: _movieControlsView];
Where _movieControlsView was set up earlier in code or in IB.
Aesthetically, you can do what you want, but I would recommend sticking with something that looks like Apple's choices so as not to confuse the user. For the project I just finished, I created a transparent button the exact size of the movie player. Clicking the button fades in a control bar on the bottom with my custom controls. If one of the controls isn't clicked, the control bar fades back out again after a few seconds.
First off, MPMoviePlayerController is a little different than MPMoviePlayer*View*Controller, so some of these answers lead to problems when converting applications that were built in an iOS 4.3+ environment.
I've built some apps using MPMoviePlayerController that worked fine when built in iOS 3.2. When I rebuilt it with XCode 3.2.6, (iOS 4.3), the videos don't even play on the iPhone. I since fixed that by adding the MPMoviePlayerController instance to a subView, then presenting a modal (Player is a UIViewController) with the movplayer in fullScreenMode:
//from didSelectRowAtIndexPath
Vid *selected = [items objectAtIndex:position];
player = [[Player alloc] init];
movplayer = [[MPMoviePlayerController alloc] initWithContentURL:selected.vidURL];
movplayer.view.frame = player.view.bounds;
movplayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[player.view addSubview:movplayer.view];
[self presentModalViewController:player animated:YES];
[movplayer setFullscreen:YES animated:NO];
[movplayer play];
[player release];
//movplayer is released inside - (void)exitedFullscreen:(NSNotification*)notification
This was done on account of the UINavigationBar being half cut off when rotating.
When I got to the iPad version of the app the modal option wouldn't work aesthetically. It was also having the UISplitViewController navBar and toolbars half cut off when rotating in full screen mode. So I tried implementing MPMoviePlayerViewController instead of MPMoviePlayerController. With this conversion, XCode gave me errors when trying to set:
movplayer.controlStyle = MPMovieControlStyleEmbedded;
The proper way to do this with a MPMoviePlayerViewController is:
movplayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
When the player is added as a subView, the pinch gestures will toggle the player between fullScreen and the size of your parentView (player.view.bounds) smoothly, as well as preserve the toolbars and navBars native to the parent.
//iPad version with a view (viewForMovie) inside the DetailViewController
movplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[current vidURL]];
movplayer.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
movplayer.view.backgroundColor = [UIColor clearColor];
movplayer.view.frame = viewForMovie.bounds;
[viewForMovie addSubview:movplayer.view];
So these two examples show some workarounds for those who want to convert their iPhone or iPad apps to a newer iOS version.
Try setting MPMovieControlStyle of your MPMoviePlayerController object to MPMovieControlStyleNone

How to play YouTube Movie on an iPhone Application when tapped on UITableViewCell?

I'm a newbie to iPhone development and just started on developing an application that contains a UITableView where each cell consisting of youtube video thumbnails in the form of webviews. For embedding YouTube Player on iPhone, I have used the follwing piece of code.
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
Now that I can see the thumbnail on the tableviewcell and once I tap on the thumbnail, YouTube player opens and play the movie.
My thumbnail occupies only a small portion of the cell and the rest of the area of the cell contains some text descriptions. My problem is that I must tap exactly on the thumbnail for the movie to play. If I tap somewhere else on the cell then it wouldn't play because my thumbnail doesn't extend all over the cell.
Isn't there a way to make the movie to play in didSelectRowAtIndexPath? I have seen some chaps suggest using Javascript but nobody seem to have an idea on the correct way of using it for this problem.
Highly appreciate it if anybody can help.
I suggest you to take a look at YouTube APIs and try to figure it out what is your real problem. but another good link is How To Play YouTube Videos Within an Application. Hope I'm helping you.
It's maybe not the best solution but It works:
//get your UIWebview in tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//...
videoView.mediaPlaybackRequiresUserAction = NO;
[videoView loadHTMLString:[self generateHtmlEmbedYouTube] baseURL:nil];
From the Documentation
mediaPlaybackRequiresUserAction:
A Boolean value that determines
whether HTML5 videos can play
automatically or require the user to
start playing them.
The onely problem with this solution is that you have to reload your UIWebView.