Launching new browser window from a custom WebKit browser view - cocoa

My MacOsX has a Cocoa app with a special controller/window that employs WebKit for browsing. I just found out that the browsed content may have links that should open new browser windows. Although I set the following two declarations (see below) for my webView - nothing happens when I click those links with my specialized window. Clicking them from a regular browser would re-open a window:
[[_webView preferences] setJavaScriptEnabled:YES];
[[_webView preferences] setJavaScriptCanOpenWindowsAutomatically:YES];
Am I missing a callback implementation?

Make yourself the UI delegate:
[webView setUIDelegate:self];
And implement webView:createWebViewWithRequest:. This method needs to create and return the new WebView object and the window to display it. If you do not want to create the new view programmatically, you can use a NIB and load that instead.

Related

High Sierra WebView blocks anchor tag navigation

I have a Mac app that displays info in a WebView. Quite an old app now and in Objective-C which I am rapidly forgetting.
The WebView contains anchor tags to allow navigation within the pages, both by clicking on links in the page displayed, or by clicking tabs in the UI which send Javascript to the WebView. In High Sierra this no longer works although it displays no error.
It appears that I need to implement isKeyExcludedFromWebScript: but my attempts to do so have failed.
Do I need to sub-class WebView? Putting isKeyExcludedFromWebScript: and isSelectorExcludedFromWebScript: in the View Controller containing the WebView doesn't work - they never get called.
If anyone has any advice or examples, I would be most grateful.
I am afraid that the answer will be to upgrade the app to use WKWebView but I was hoping for a quick work-around until I get time to do that.
Answering my own question here as I have worked out a solution.
The problem was that I was loading an HTML string into the WebView and when I tried to use internal navigation links, it didn't have a base URL to use as a prefix and so the anchor navigation never worked.
It used to work, prior to High Sierra, so there must be something new about how WebViews operate.
The solution was to save the HTML string to a temporary file and have the WebView load that file's URL instead of loading the string directly.
This applies to both WebView and WKWebView.

How to disable "Open in external viewer" and "Download" menu automatically showed from webview when a pdf is loaded?

I want disable the "open" end "download" functionality from this menu in cocoa WebView, or alternatively disable entire menu.
(source: federicocappelli.net)
There's a slim chance that if you implement a WebUIDelegate whose implementation of webView:contextMenuItemsForElement:defaultMenuItems: filters out WebMenuItemTagOpenWithDefaultApplication and WebMenuItemTagDownloadImageToDisk then that would prevent the web view from showing those two options in the overlay.
Otherwise, you can try subclassing WebView and overriding -mouseMoved: to swallow those events. That might interfere with normal functioning, though.
Or maybe you can override -addTrackingArea: to prevent the adding of whatever tracking area is used to show the overlay.

WebView blank after reopening NSWindow

I am using a WebView to display some incoming content to users in a single-window application.
There is a single window controller in the app delegate that I use to send -showWindow: on -applicationDidFinishLaunching: and -applicationShouldHandleReopen:hasVisibleWindows: notifications.
This works well until I close the window and click the dock icon to reopen the window.
At this point the web view is blank and no longer responds to mouse input, such as the scroll wheel. The scroll view still indicates the apparent size of the document.
The window is not released on close, according to IB.
Am I missing something with regards to keeping that content around?
According to the documentation WebView is closed with window. However, we can subclass WebView and override shouldCloseWithWindow and return NO.
- (BOOL)shouldCloseWithWindow

Is WebView behavior identical to Safari

Our Mac OS application displays user interface inside WebView component.
Can we rely on the fact that WebView behaves exactly as Safari content pane on any Mac?
Can we expect the same set of plugins installed in Safari and inside WebView of our application?
In other words, is the same WebView shared by all applications on Mac OS, including Safari?
Quoted from apple docs:
A WebView object is intended to
support most features you would expect
in a web browser except that it
doesn’t implement the specific user
interface for those features. You are
responsible for implementing the user
interface objects such as status bars,
toolbars, buttons, and text fields.
For example, a WebView object manages
a back-forward list by default, and
has goBack: and goForward: action
methods. It is your responsibility to
create the buttons that would send
theses action messages. Note, there is
some overhead in maintaining a
back-forward list and page cache, so
you should disable it if your
application doesn’t use it."
A WebView uses webkit engine to render html which is what safari also uses. Hence most of the functionality will be the same.

Using Webkit to create Skinable custom windows on OSX

There are tutorials for using a webkit view inside a cocoa application to achieve skinnable contents, but what would I do if I wanted to use webkit to create custom, skinnable windows?
Examples would be dashboard widgets, or
BowTie
Beware, I'm a noob.
I've never tried it, but I think you'd just set a WebView as the content view of a transparent borderless window, and tell the WebView not to draw a background. That way, the content of the WebView would define the window boundary.
You create a borderless window by passing NSBorderlessWindowMask to the -initWithContentRect:styleMask:backing:defer: method of NSWindow, and you can set its background to transparent by calling [window setBackgroundColor:[NSColor clearColor]].
You'd have to handle dragging of the window etc yourself. It will probably get a bit messy.
To be honest, it's not something I'd attempt as a first Cocoa project.

Resources