Cocoa: clicking on "Watch on YouTube" inside WebView does nothing

I hava a WebView with embedded YouTube video. The video plays perfectly, and everything about it works, except when you click "Watch on Youtube".

I want the “Watch on YouTube” button to open the YouTube video in the default browser, but it currently does nothing.

I have a WebPolicyDelegate installed on a WebView , but not one of them:

 - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener` 

neither

 - (void)webView:(WebView *)webView decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id<WebPolicyDecisionListener>)listener 

Called when the "Watch on YouTube" button is clicked.

How can I detect that the Watch on YouTube button is clicked so that I can open it in my default browser?

+4
source share
1 answer

The Watch YouTube button navigates in a new tab / window, so you need to implement the WebUIDelegate -webView:createWebViewWithRequest: method. You must infer from the request URL whether the request was triggered by the Watch on YouTube button or not. Once you know this, you can tell the default browser to open the URL and return nil from your delegate method to tell WebKit that you are denying navigation.

+1
source

Source: https://habr.com/ru/post/1392130/


All Articles