JMLdev
JMLdev

Reputation: 846

iOS how to tell when webViewDidFinishLoad has completed?

I have some code inside the webViewDidFinishLoad delegate method, and I need some way to tell that this code has finished executing from within another method in a completely different part of my code. Is this possible? Basically what I want to do is something like this:

-(void) myMethod
{
    // do stuff that causes webViewDidFinishLoad to be called

    if (webViewDidFinishLoad has finished executing)
    {
        // do stuff with outputString
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    outputString = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"myJSFunction('%@')",jsInputString]]; 
}

I looked at this answer already and it made no sense to me.

I should add that inside myMethod, I can be sure that webViewDidFinishLoad will only be called once.

Upvotes: 0

Views: 3065

Answers (6)

Carl Veazey
Carl Veazey

Reputation: 18363

Try to avoid waiting for delegates and notifications in other methods - do the work that depends on them when they call back to your class. Don't call myMethod until you get the delegate message. If myMethod is called from user input, disable the associated controls until after the web view loads.

Upvotes: 1

smoothdvd
smoothdvd

Reputation: 2448

In webViewDidFinishLoad, you post a notification to your "myMethod" code part. Something like this:

in UIWebView:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"webview_finished" object:self userInfo:nil];
}

in your "myMethod" code part:

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:@"webview_finished" object:nil];
}

Upvotes: 0

brush51
brush51

Reputation: 5781

use a Boolean and set its value to YES in the last line of webViewDidFinishLoad.

then check in your myMethod like this:

-(void) myMethod
{
    // do stuff that causes webViewDidFinishLoad to be called

  if (yourBoolean == YES)
  {
      // do more stuff
  }
}  

that should do your work.

Upvotes: 0

Mil0R3
Mil0R3

Reputation: 3956

Why not try GCD?

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // WebView load content here 

    dispatch_async( dispatch_get_main_queue(), ^{

       // load myMethod

    });
});

Upvotes: 0

Sisi
Sisi

Reputation: 263

You can always set a boolean to indicate whether the webview has finished loading. And the value of the boolean will be changed in the webview delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    loadFinished = YES;
}

Then in your own method, check "loadFinished" will do the task. You have to set <UIWebViewDelegate> for interface to use this delegate method

Upvotes: 2

Matthias
Matthias

Reputation: 8180

  • You could use UIWebView's iVar loading
  • You could use your own shared boolean flag as iVar and set it in webViewDidFinishLoad.

However, maybe you could reconsider your design and do in webViewDidFinishLoad what to do after loading is finished.

Upvotes: 0

Related Questions