Cocoa: Clicking on "Watch on YouTube" inside a WebView does nothing - cocoa

I hava a WebView with an embedded YouTube video in it. The video plays fine and everything about it works except when clicking on "Watch on Youtube".
I'd like the "Watch on YouTube" button to open the YouTube video on the default browser but currently it just doesn't do anything.
I have a WebPolicyDelegate set on the WebView but neither:
- (void)webView:(WebView *)webView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id<WebPolicyDecisionListener>)listener`
nor:
- (void)webView:(WebView *)webView
decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
newFrameName:(NSString *)frameName
decisionListener:(id<WebPolicyDecisionListener>)listener
are called when the "Watch on YouTube" button is clicked.
How could I detect that the "Watch on YouTube" button was clicked so I can open it on the default browser?

The "Watch on YouTube" button performs its navigation in a new tab/window, so you'll need to implement WebUIDelegate's -webView:createWebViewWithRequest: method. You'll have to infer from the request's URL whether the request was initiated by the "Watch on YouTube" button or not. Once you know that, you can tell the default browser to open the URL and return nil from your delegate method to tell WebKit that you're disallowing the navigation.

Related

How can you get input file to work in a WKWebView?

When you have a WKWebView in an OSX application, when you press a <input type="file"> button, you can't select a file from your harddisk. How would you enable this feature?
I heard that normally you use:
func webView(sender: WebView!, runOpenPanelForFileButtonWithResultListener resultListener: WebOpenPanelResultListener!)
Which is part of the WebUIDelegate but altho you set self.webView.UIDelegate = self it does not get fired.
Here is the 100% working solution for WKWebview "File Upload" Problem.
You just need to implement the UIDelegate:
- (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> *URLs))completionHandler;
Follow this link for more description about this delegate:
RunOpenPanel Delegate
This is a known bug in WKWebView.
https://bugs.webkit.org/show_bug.cgi?id=137759

WebView loads slowly in Mac OS

I have created plugin for Mail.app on Mac OS.
I'm using WebView to display web pages, all would be fine but web pages load slowly.
Then I created test cocoa application to compare loading time.
I was surprised when the test application loads page ~5 times faster.
In developer bar I saw my test application receives 304 code that indicates "the resource for the requested URL has not changed and cached resource can be used".
In contrast to the test application the plugin always receives 200 http code and loads resource again.
Maybe I should specify to use a cache in the webview, or I have some bundle permissions problems.
In the plugin, I tried to specify SharedURLCache like this
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*20
diskCapacity:1024*1024*5
diskPath:NSHomeDirectory()];
[NSURLCache setSharedURLCache:cache];
Then I tried subscribe to the ResourceLoadDelegate on the WebView and change request object like this
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
{
if ([request cachePolicy] != NSURLRequestReturnCacheDataElseLoad)
{
return [NSURLRequest requestWithURL:[request URL]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:[request timeoutInterval]];
} else {
return request;
}
}
Also I tried to change properties on WebView
[[webView preferences] setUsesPageCache:YES];
[[webView preferences] setCacheModel:WebCacheModelPrimaryWebBrowser];
but it's all not working.
Thanks for help.

Catch mailto links in WebView

Is there a method to this madness? I am trying to build a browser app for a kiosk that restrict much need for running additional applications and simply stay within one website.
I research and found decidePolicyForNavigationAction should work for what I want, but how do I start filtering URI schemes (mailto://, irc://, etc.)? Thanks!
You're implementing a WebView in your application to browse the web, right?
If yes, look into the WebPolicyDelegate Protocol reference.
Especially the following delegate might be of interest:
- (void)webView:(WebView *)webView
decidePolicyForNewWindowAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
newFrameName:(NSString *)frameName
decisionListener:(id < WebPolicyDecisionListener >)listener
Using the above delegate, you can validate any request, including mailto requests.
Quick example how to detect the URL scheme and decide wether to block:
NSLog(#"Request URL scheme = %#",[[request URL] scheme]);
if([[[request URL] scheme]isEqualToString:#"mailto"])
{
[listener ignore]; // Block Request
}
else
{
[listener use]; // Allow Request
}

MacOSX Custom Webkit view + Flash content with clickable links

Symptom:
View the flash website with any browser, the links inside the flash content work and launch a new window
view the flash with my custom WebView, click the same links. No response.
I tried delegating "decidePolicyForNewWindowAction" and "decidePolicyForNavigationAction" but ironically, these are only called after I launch a URL from my code, NOT as a response to the user clicking the link from the page.
I am sure the fact this is a flash app content has something to do with it. I don't know how to resolve this. Help will be appreciated.
You'll need to set a UIDelegate for the WebView, and then implement - (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request in that delegate.
If you then set a policy delegate for the new WebView that you return (that WebView can be in a hidden window), the delegate method that will be called is:
- (void)webView:(WebView *)aWebView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener`
The URL to which the Flash app is trying to send the window is in the WebActionOriginalURLKey of the actionInformation dictionary, and you can at that point decide if you want the navigation to proceed, or send an ignore to the WebPolicyDecisionListener and handle the URL some other way.

WebView integration

I am wondering how to integrate a WebView in a cocoa application:
- How to call javascript function from cocoa in the WebView
- How to handle in cocoa the click in a link inside the WebView
...
Thanks in advance for your help
First of all, there is no NSWebView in cocoa, it's called UIWebView on the iphone and WebView on Mac.
If you want to simply load a page with the webview, create an NSURLRequest (using NSURL) then call - (void)loadRequest:(NSURLRequest *)request
If you want to use javascript functions, just make an NSString containing your script, then call - (NSString*)stringByEvaluatingJavaScriptFromString:(NSString *)script.
I don't fully understand why you want to handle a click inside the webview... If you want to detect a redirect, there is a delegate method that can help you:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Resources