Embed YouTube with WKWebView [duplicate] - objective-c

This question already has answers here:
How to make a custom view resize with the window with Cocoa Auto Layout?
(4 answers)
Closed 2 years ago.
I am very new to MacOS Objective-C Coding. I know there are a lot of solutions for iOS on displaying a YouTube video, but I am attempting with MacOS and Objective C.
So far, I am using WKWebView to display a url. This work fine, except I more wanted to display a youtube video full screen in the window (and just the video, not the whole youtube webpage along with the video).
- (void)windowDidLoad {
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.webNSView.frame configuration:theConfiguration];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:#"https://www.youtube.com/watch?v=t168mpf0SxU"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.webNSView addSubview:webView];
My solution was to use embed instead of a direct url.
NSURL *nsurl=[NSURL URLWithString:#"https://www.youtube.com/embed/t168mpf0SxU"];
However, I resize the window the video doesn't resize the webview embed with the window. Is there a piece of code I'm missing for that?

My solution was to first set Auto Resize Mask Constraints:
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
Then to add layout constraints:
[self.webNSView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
[self.webNSView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[webView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(webView)]];
This seems to resize the WKWebView inside a NSView

Related

How to align UIWebView video (from online DB) to center : Xcode

I am displaying a video to my UIWebView from an online Database as:
//-------- Creating webview --------//
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 145, 300,190)];
webview.scalesPageToFit = YES;
webview.backgroundColor = [UIColor redColor];
webview.opaque = NO;
[self.view addSubview:webview];
//-------- Loading Video --------//
NSURL *fileURL = [NSURL URLWithString:
[NSString stringWithFormat:#"http://mysite.org/myProject/sample.php?name=123746"]];
NSURL *nsurl=fileURL;
[webview loadRequest:nsrequest];
It is working great. But the video looks like below:
So, Is there is any way to align the video to center of the web view? Anyone please help. thanks.
3 Options.
If the video will always be that size, make the UIWebView just slightly bigger and center that.
Use [webView stringByEvaluatingJavaScriptFromString:#""] to pass in javascript to adjust the css to center it.
If its a webpage you control, add css to the HTML to center the video div in the window
as I was not able to find any good inbuilt solution using Xcode, I have solved the issue with the help of backend and HTML iframes. I've saved the exact url in a data base and using HTML <iframe>, I loaded the video in the webview with tag to the iframes.

video not playing ios 6

I am trying to play videos from url which my web service returns using MPMoviePlayerViewController. A sample url returned is:
http://view.vzaar.com/1128188/player
Which i pass to MPMoviePlayerViewController. Do you think this url is compatible to be played?
I looked at the source of that page, and it's an html page. I did find a html5 source url. Try:
http://view.vzaar.com/1128188/video
Yes, with above url. I just tried it in xcode and I got it to work.
NSURL *url = [[NSURL alloc] initWithString:#"http://view.vzaar.com/1128188/video"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.moviePlayer prepareToPlay];
[self.moviePlayer.view setFrame:self.view.bounds]; // player's frame must match parent's
[self.view addSubview: self.moviePlayer.view];
[self.moviePlayer play];
No. It can't be played because it's an HTML page with a flash player embedded. Change your web service to return the video directly, possibly in a different URL.

Mac App WebView background

How is it possible to set the an image as background of the WebView in a Mac App? Because the WebView has that "bouncing" effect, if you scroll further down than the content, the background is usually just white. Is it possible to set a Background-image like it is possible for the UIWebView?
EDIT: The alternate way would be to completely deactivate the bouncing-effect of the webView.
If you are creating this webview programically then create an image then create the webview. Make sure their positions (x,y) and sizes (height, width) are the same.
If you are doing this through IB put down an image (litterally just drag an image) then put the webview on top of it.
Here is a way I found that stops the bouncing:
id sub = self.web.subviews[0];
if ([sub isKindOfClass:[WebFrameView class]])
[sub setAllowsScrolling:NO];
The class check may not be needed, but I like to make sure.
We can add background image in WebView using cocoa. to do this we need to add css for body tag.
Create a webview:
WebView *webView;
Fetch image form bundle path:
NSString *path = [[NSBundle mainBundle] pathForResource:#"my-bg" ofType:#"jpg"];
write Css for body tag.
NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:#"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:#" background-image: url('file://%#');",path]];
[htmlBuilder appendString:#"}"];
Load this image on webview
[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];
Your image will be added in background on webview.

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.

Setting NSBox's setContentView with NSImageView not working

I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!