Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a WebView to act exactly like Safari

(edited to give a better description of the answer I gave)

I have created a very simple browser in InterfaceBuilder consisting of a nav bar and a webview.

Everything works fine except when I try to tab between input fields is the webview, the focus goes to the nav bar.

I'm assuming I need to do something with the responder chain, but I haven't been able to figure out what.

Any advice?

Thanks, Kelly

like image 990
Kelly McDonald Avatar asked Dec 07 '10 12:12

Kelly McDonald


1 Answers

There is probably no one else in the world who cares about this, but I'm going to answer it in case someone needs this at some point.

As I mentioned in the comments, I am using the cappuccino framework and I was actually having 3 different problems.

Let me give you the set up here.

I was creating a cocoa app with a WebView embedded and loading a page with a cappuccino app in it. At first tabbing between fields didn't quite work for any form on any webpage.

  1. As I said in the comments, if you are using IB, you can set the windows 'initialFirstResponder' to be the webview, and at least 'normal' forms work correctly. Cappuccino still did not.
  2. Second was the problem where keyCode and charCode in the dom event was different from WebView in cocoa to Safari. It turns out that there is something called 'keyboard quirks' mode. If you look for _needsKeyboardEventDisambiguationQuirks in the WebView ObjC class source you will see that it makes an exception for safari, turning it off. It is supposed to turn it off for older versions of WebKit, but it doesn't. see webkit bug 32694 To solve this, I had to do two different things. a) override _needsKeyboardEventDisambiguationQuirks so that it returns NO b) if the WebView is already instantiated when you override that method, you will need to also call [[webviewinstance preferences] _postPreferencesChangesNotification] in order to get it to work. This then makes your WebView send the DOM Events like Safari.

  3. Finally, cappuccino was looking at the user agent string to determine how to handle some stuff. It was checking for WebKit and for Safari. I cribbed the safari user agent string and set that as a custom user agent string for the WebView.

    [mywebviewinstance setCustomUserAgent: @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4"]
    

Finally, everything worked just like it did in safari.

Hope this helps someone!

like image 56
Kelly McDonald Avatar answered Nov 01 '22 16:11

Kelly McDonald