views:

420

answers:

3

I would like to use a UIWebView object to run custom javascript methods against a local web page. I can do that, but I would like to do it in the background, I mean, while I'm showing a navigation controller or any other content, load webpages and call javascript methods on them.

How would do that?

+2  A: 

I'm not sure what you mean by "in the background" but if you simply want the UIWebView to not be visible, this property is available on all UIView objects:

@property(nonatomic, getter=isHidden) BOOL hidden

Straght from the class reference:

A hidden view disappears from its window and does not receive input events. It remains in its superview’s list of subviews, however, and participates in autoresizing as usual. Hiding a view with subviews has the effect of hiding those subviews and any view descendants they might have. This effect is implicit and does not alter the hidden state of the receiver’s descendants.

Hiding the view that is the window’s current first responder causes the view’s next valid key view to become the new first responder.

The value of this property reflects the state of the receiver only and does not account for the state of the receiver’s ancestors in the view hierarchy. Thus this property can be NO if the receiver is hidden because an ancestor is hidden.

slf
A: 

Once you intialized the webview, d you can easily kick off a background thread, that works the view and loads the page.

[NSThread detachNewThreadSelector:@selector(connectToServer) toTarget:self
             withObject:nil];

.....

(void) connectToServer
{
    // in a different thread....so we need a autoreleasepool
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    ... // do your stuff
    // load HTML on webview
   [autoreleasepool release];
}
sujee
A: 

I was able to do this just by creating a web view in memory and never displaying it. It still exists and works fine.

self.backgroundWebView = [[[UIWebView alloc] initWithFrame:CGRectZero] autorelease];
[self.backgroundWebView loadRequest:[NSURLRequest requestWithURL:myURL]];
Dave Batton