1

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
vane
  • 99
  • 6
  • What exactly do you mean by "in console"? –  Aug 29 '17 at 01:06
  • What do you mean by "finish the console" ? You want to remove the Webview? – Kosuke Ogawa Aug 29 '17 at 01:09
  • sorry ,my program is a command line tool ,so after call [webView loadRequest:request],the program is over 。i just want to get the result in - (void)webViewDidFinishLoad:(UIWebView *)webView. so i think i should add do something after call [webView loadRequest:request]. – vane Aug 29 '17 at 01:18

2 Answers2

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • See https://stackoverflow.com/questions/2154600/run-nsrunloop-in-a-cocoa-command-line-program – matt Aug 29 '17 at 01:35
0

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);
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • yes ,i just do it ,but after call [webView loadRequest:request],the command line tool is over, so how can i wait for webViewDidFinishLoad is invoked?I am try to do this : [NSThread sleepForTimeInterval:5]; but it's invalid. – vane Aug 29 '17 at 01:31