Use UIWebView in console

102 views Asked by At

I want to use UIWebView in the console, so I define a delegate:

@interface webViewDelegate : NSObject <UIWebViewDelegate>

and write the protocol:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;

and then loadRequest:

webView = [[UIWebView alloc] init];
webView.delegate = WVdelegate;
NSURL *htmlURL = [NSURL URLWithString:@"http://192.168.0.100"];
NSURLRequest *request = [NSURLRequest requestWithURL:htmlURL];
[webView loadRequest:request];

The problem is that this is a console, and I want to finish the console after the delegate has been invoked. What should I do to wait for the delegate after calling loadRequest?

2

There are 2 answers

1
Kosuke Ogawa On

You can get HTML source in console:

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    // Check here if still webview is loding the content
    if (webView.isLoading)
        return;

    NSString *fullHtml = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"];
    NSLog(@"html:%@", fullHtml);
}
1
matt On

The problem is that you have no runloop. Thus, when your code comes to an end, the command-line tool comes to an end; things do not persist long enough for the asynchronous code to run.