WebView questions

Hey all, I’ve got a few questions about WebViews that I’ve not been able to find the answers to yet! If anyone here has the answers to one or many of the questions, I’d be grateful!

  1. Where are cache files stored? It seems that my apps with WebViews store things in a different cache from Safari. While this does make a degree of sense, how does one clear said cache at a fresh launch of the app (if not on demand!)?

  2. Can one append an identifier to the user agent string? I’d love to be able to identify the source of hits from my apps. e.g. adding “(lewellyn’s super-duper thinger!)” to the UA.

  3. Can one programatically (preferably via pure AppleScript) either reinstantiate the WebView, or clear the history for it? When I “recycle” the WebView, I don’t want the previous history to be available.

  4. Can one actually allow Flash-based popups to create new windows somehow? AFAICT, I selected the proper item in IB, but I can’t actually get Flash objects which spawn a new browser window (e.g. to display a standard “purchase” screen) to actually do so. I’d be satisfied with re-using a single window over and over again, if necessary. (Subject to #3, above.)

  5. In relation to #4, is there a way to allow a Flash object to close the window? They work fine in Safari for the purpose, but I’m not sure what connection(s) would need to be made for a WebView. I suspect some ObjC in my future. :frowning: (I’ve been trying to stay pure AppleScript, but I suppose that once an app hits .5K LOC you start needing to extend it externally. And, if I get answers to all the above, who knows if I may end up at 1K! So much for what started out as “let’s dynamically create a web page and feed it to Safari!” :lol:)

Any and all feedback is most appreciated. I can elaborate on any of the points, if need be. But, I suspect that my desires are obvious enough from each of the above. I’m not looking to add complexity to my app; indeed, this should actually serve to reduce complexity on the user end!

And if any of the above points above have been beaten like a dead horse, previously, I’d appreciate links. I certainly didn’t find any! :frowning: (And I’m still trying to fight with the ADC docs. For some reason, I find them hard to grok; I suspect it’s my Unixy roots. I never really “got” them in the “Classic” days, either.)

Hi Lewellyn,

here some input - hope it will help you a little bit …

pure AppleScript? I don’t think that’s possible but clearing the history of your web view should be possible with some call methods:

        set history to call method "backForwardList" of myWebView
        set capacity to call method "capacity" of history
        call method "setCapacity:" of history with parameter 0
        call method "setCapacity:" of history with parameter capacity

or to completely disable the history:

call method "setMaintainsBackForwardList:" of myWebView with parameter NO

Not sure if I understood your problem here - do you want to know how to have your flash/actionscript open a new window (that’s usually done by a getURL with some javascript as far as I remember) or is it the web view part of receiving this request and creating the new browser window? For the latter your web view needs a UIDelegate. I don’t think there is any AppleScript access to this. You will probably have to use some Objective-C code for this (and reconnect it to your script if necessary).

same here - this can be caught by an UIDelegate of your web view.

Here a very simple example for a delegate that handles window.open() and window.close():

(all steps with Xcode 3 / IB 3):

  1. In Xcode create a new file of the type ‘Cocoa → Objective-C class’, name it ‘UIDelegate’ (for example)
  2. switch to Interface Builder and drag an ‘Object’-Instance to your nib - you find it in Library → Objects & Controllers → Controllers → Object
  3. select the Object instance (blue cube) and open the classes tab of the inspector (Command+6) - choose the new ‘UIDelegate’ class as class.
  4. Ctrl-Drag a connection from your web view to the controller instance (UIDelegate) and select ‘UIDelegate’ from the menu that pops up
  5. go to Xcode and edit the code fro your Controller:

[code]// UIController.m

#import “UIDelegate.h”

@implementation UIDelegate

  • (void)webViewClose:(WebView *)sender{ // there was a request for a window.close
    [[sender window] orderOut:self]; // close the window of the web view

  • (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request{ // a request to open a new web view
    return (sender); // we redirect it to the ‘sender’ - means it will open the request in the calling web view


Thanks for your insightful reply! I’m going to have to try a few of these things out now! :smiley:

That’s pure enough AppleScript for me! Methods are a bit different than do shell script or Objective-C glue. :lol: One of these days, I’ve gotta sit down and learn Obj-C. It can’t be THAT different from good ol’ ANSI C, can it? I mean, people seem to like it more than C++! :smiley:

Ya. The Flash spawns a new browser window just fine when used with a “normal” web browser. As I only have access to the compiled Flash, I can’t say for certain how the windows are spawned. (Indeed, my Flash knowledge is limited enough that all I know for certain is “they open”!) I’ll look into UIDelegate and see if I can’t figure out what needs to be done. Whether each popup request spawns a new window or re-uses a single one, I really don’t care. Hopefully UIDelegate makes the decision for me! :lol:

Of course, the code you gave for the next part will really help me out, I think, toward this! (Re-reading the code just before posting, it looks like it may do the trick as-is! Wow!)

Most excellent! Thank you so much!

Whilst reading ADC documentation earlier, I think I stumbled across how to clear the cache; I will have to try that later, as well. And, as for the UA spoofing, I think I found something most interesting in WebView.h!

As much as I was hoping for a solution that consisted of my nib, the default main.m, and some AppleScript, I think it’s time to let this project grow. I think I’ve hit the limits of what I can really do within AppleScript; I’ve got many lines which are at the point where you look at them and say “Um, there are some English words in there, yes.”. And that’s a sure sign that you’re starting to manipulate AppleScript into an unreadable mess. :frowning:

I’ll post back once I have more info available on my progress! :smiley: