views:

223

answers:

3

What I'm actually trying to do is put a WebKitView into a ScreenSaver (which inherits NSView). I'm totally new to MacOS X and Cocoa (but I'm very familiar with Objective-C and used some parts of GNUStep). Do I need some laying out? I want to show only one control in the whole NSView.

A: 
[aScreenSaverView addSubview:aWebKitView];

But why add a UIWebView into a screen saver view when you can just make the UIWebView take up the full screen on its own? Introducing view hierarchies where they are not needed is not a good idea because it increases the processing needed to display the interface.

Marc W
I've tried [aScreenSaverView addSubview: aButton] and it didn't work. Am I missing something or webkit views are just cooler that buttons?I don't understand what you say about making UIWebView take up the full screen, doesn't a screen saver have to inherit ScreenSaver?
J. Pablo Fernández
he writes a screen saver from screensaver framework, not iphone application
Oh my bad. I must have misread your question. Apologies.
Marc W
+3  A: 

In your initWithFrame:isPreview: method, create a WebView in the usual way, then, send yourself an addSubview: message, passing the web view:

webView = [[WebView alloc] initWithFrame:frame];
[self addSubview:webView];

If you're wondering what rectangle you should pass as the WebView's frame, read the View Programming Guide. Also, don't forget to release the webView in dealloc (or autorelease it in initWithFrame:isPreview:).

Once you have your web view, you'll need to load a page into it. Set a suitably long animation interval (at least a couple of seconds), and load the page in animateOneFrame:

- (void) animateOneFrame {
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
}

You may also want to handle WebViewProgressFinishedNotification, and put off re-loading the web view until that happens (to compensate for slow or soaked connections). You'll do this with an instance variable, which you set to YES in both initWithFrame:isPreview: and your notification-handler method, and test and set to NO in animateOneFrame:

- (void) animateOneFrame {
    if (hasFinished) {
        [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
        hasFinished = NO;
    }
}
Peter Hosey
I'm just starting so my Screen Saver can be anything. I've created it with Xcode and the Screen Saver Standard Apple plug in, but the project didn't came with any nib or nix file, only a View.h and a View.m. I'd be perfectly happy to work with a nib file, how do I do that?Thanks Peter.
J. Pablo Fernández
Sorry, it's been a long time since I worked with ScreenSaver framework. I've edited my answer.
Peter Hosey
Thanks for the answer, looks promising.
J. Pablo Fernández
Peter, I edited, kinda heavily, your answer because it set me in the right direction but I wanted to fill in some details. I wanted the selected answer to be yours because you set me in the right direction. If you feel it was too much I can write my own answer separately.
J. Pablo Fernández
I made some further edits to clarify and expand the information you added, and unify it with my own style.
Peter Hosey
A: 

You can also not worry too much about your animation interval by calling

[self stopAnimation];

at the end of your animateOneFrame method.

Dave Martorana