I have a simple NSView that hosts a WebView.
When I try to make the view layer backed, the WebView stops rendering content - all it renders are the scroll bars.
For simplicity, I added the following code to the applicationDidFinishLaunching method of the app delegate of a brand new xcode project :-
NSView* view = [window contentView];
[view setWantsLayer:YES]; // This is the problematic line!
WebView* webView = [[WebView alloc] initWithFrame:NSMakeRect(0,0,400,400)];
WebFrame* mainFrame = [webView mainFrame];
[view addSubview:webView];
[mainFrame loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
If I leave out setWantsLayered the WebKit renders the web page. If I set it, WebKit just renders a white square with scroll bars.
Layer backed WebView's aren't supported. From the Leopard release notes:
Most of the standard views and controls that AppKit and Mac OS X's other Cocoa frameworks provide are able to function in layer-backed mode in Leopard, with the exception of certain specialized views such as WebKit WebViews and Quartz Composer QCViews, whose use in layer-backed mode is not presently supported.
(http://developer.apple.com/mac/library/releasenotes/cocoa/AppKitOlderNotes.html#Animation - Last paragraph of the "New View Animation Facilities, and Layer-Backed View Drawing" section)
You should file a bug with Apple and reference rdar://5270371 as found in this mailing list post http://lists.apple.com/archives/Webkitsdk-dev/2007/Dec/msg00042.html.
This now seems to work.
I have just tried out the same code on Mountain Lion and all is OK.
Related
I created a simple test app where one field is an NSTextField and another is a <textarea> instead a WKWebView. I can click into the web view once, but if I go back to the NSTextField, I'm unable to get the cursor back into the web view ever again.
Here's a demonstration:
As you can see, the web view is still interactive to some extent, it just can't become (I assume) the first responder again.
Is there something weird about WKWebView and the responder chain? How can I make the WebWebView behave like a normal NSTextField?
Here is a sample project built in Xcode 11.5 in case you want to try it out.
How can this be fixed?
The issue seems to be related to the textarea, using content editable <body contenteditable="true"></body> does seem to work.
A workaround for using the textarea is to subclass WKWebView like this:
- (void)mouseDown:(NSEvent *)event
{
[super mouseDown:event];
[[self window] makeFirstResponder:self];
}
I'm a noob OSX Developer having issues getting a WebView to load a URL when inside of a Custom View using NSPopover...My NSPopover is activated when a notification bar item is clicked.
This code is in my application delegate under
"- (void)applicationDidFinishLaunching:(NSNotification *)aNotification":
NSURL*url=[NSURL URLWithString:#"http://www.google.com"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
[[_webView mainFrame] loadRequest:request];
It works just fine if I move the WebView to any other window, but the WebView in my Custom View for this NSPopover displays as a blank page. Any ideas why? Any additional information you might need? Limitation of the CustomView?
You have created the Outlet in IB for the UIWebView in your Custom View ? and strung it together?
I intend to replace QT webkit with OSX native webkit in a QT based application, what I did right now is making an QT control inherited from QMacCocoaViewContainer and embed a cocoa web view in it. It could show the page correctly, but cocoa web view can't receive mouse moved event, so the js/css hover effect can't be shown. I tried to compare it with native cocoa Application, and found that cocoa web view could get NSMouseMovedNotifcation from notification center in native cocoa App, but QT app builds its own event loop and send event with its own strategy, this notification isn't received.
Who can tell me how to fix it? Any comments are very appreciated.
QTWebView(QWidget *pparent): QMacCocoaViewContainer(0, pparent)
{
...
xwebview = [[WebView alloc] init];
setCocoaView(xwebview);
NSString *urldefault = #"http://www.google.com";
[[xwebview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urldefault]]];
...
}
I was having a similar issue and I resolved it by implementing the macEvent(EventHandlerCallerRef, EventRef) function. Create an NSEvent from the eventRef
NSEvent* nsEvent = [NSEvent eventWithEventRef:event];
If the event is a MouseMoved event then send a mouseMoved: event to the NSView where the NSView is (NSView*)winId().
I hope this helps
I am trying to build an MacOS app which create an OpenGL view and embed it to webview as it's element (using OpenGL instead of WebGL). The main goal is to use the web (html/javascript) as UI and draw the result with OpenGL view. (besides, the OpenGL view will also handle the user input)
The issue is the overlay between the webview and OpenGL view. If OpenGL view on top of webview, the webview UI (like: dropdown box) possible cut by the OpenGL view. The webview on top of OpenGL view, all of the webview view will block the OpenGL view.
Is there any solution to solve this? (OpenGL and Webview)
WebKit has an interesting functionality where you can associate webviews with certain MIME types to your own custom NSView classes.
In your HTML webview, you can put an iframe which has a custom mimetype (e.g. "application/my-custom-view").
After that, from your native app, you can call:
[WebView registerViewClass:[MyCustomView class] representationClass:[MyCustomRepresentation class] forMIMEType:#"application/my-custom-view"];
See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Webkit/Classes/WebView_Class/Reference/Reference.html#//apple_ref/occ/clm/WebView/registerViewClass:representationClass:forMIMEType:
MyCustomRepresentation can be pretty lightweight, my Representation class doesn't do anything, it just defines stub delegate functions.
I'll preface this by saying this is my first Mac application, though I've been building iOs apps for some time.
I've got a menu bar app (system tray app) by which I mean I've got this.
I now want to show an NSWindow I've created in Interface Builder so I've created a class that's derived from NSWindow. Made my class the Window's delegate and from the App Delegate I do this:
MyClass *myClass = [[MyClass alloc] init];
[myClass display];
[myClass center];
[NSApp activateIgnoringOtherApps:YES];
[viewer makeKeyAndOrderFront:nil];
This seems to show a window without the standard window buttons (minimise, maximise, close) rather than the window I've defined.
Is this the right way to be showing windows? and how should the Window be defined so it shows my designed interface?
In Windows Forms programming this would be:
Form myForm = new Form();
myForm.Show();
You rarely need to subclass NSWindow unless you want to override a window's behavior. A more typical usage scenario would be to use an instance of NSWindowController or a subclass of NSWindowController to manage the window, by making that class the File's Owner in Interface Builder. Once this is done, to grab an instance of the window use:
NSWindowController *wc = [[NSWindowController alloc] initWithWindowNibName:#"NIBNAMEHERE"];
[wc showWindow:nil];
Another alternative in your app would be to add an NSWindow IBOutlet to your app delegate, and load the window in your app delegate with:
[NSBundle loadNibNamed:#"NIBFILENAME" owner:self];
[_window makeKeyAndOrderFront];