Reputation: 671
I am very new so bear with me. I had an youtube video embed in a WKWebView playing fine on macOS in Objective-C. At first, none of the standard youtube links on the video (channel, recommended videos, etc) would load. I think this was because youtube uses _blank target links. The following code fixed it so any video links will now open in the WKWebView.
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration %@ %@", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[(WKWebView *)_webView loadRequest:navigationAction.request];
}
return nil;
}
However, I would like these links to open in the macOS browser, and not WKWebview. Lot's of swift examples on iOS, but can't seem to get links to open from WKWebView in safari.
I have tried:
if (!navigationAction.targetFrame.isMainFrame) {
[[NSApplication sharedApplication] openURL:[navigationAction.request URL]];
}
But doesn't work on macOS
Upvotes: 1
Views: 359
Reputation: 671
This is what eventually worked for me. Seems to open any url (including javascript type popovers used in YouTube) in Safari instead of opening them in WKWebView (or not opening them at all).
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration %@ %@", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[[NSWorkspace sharedWorkspace] openURL:[navigationAction.request URL]];
}
return nil;
}
Upvotes: 2
Reputation: 33
Not 100% sure but I've seen this circulated online for opening URLs on Mac. Give it a try.
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
Upvotes: 0