How to wait until webViewDidFinishLoad is called?

429 Views Asked by At

I'm creating an iOS app.

I separated a PDF which has multi page into each pages,

And then create UIWebViews to show each pages.

The code is like this.

for( int i = 0; i < _pageNumbers; i++)
{
    _webView = [[UIWebView alloc]init];
    _webView.delegate = self;
    _webView.frame = imageViewFrame;
    _webView.scalesPageToFit = YES;
    _webView.autoresizingMask =
    UIViewAutoresizingFlexibleLeftMargin  |
    UIViewAutoresizingFlexibleWidth       |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin   |
    UIViewAutoresizingFlexibleHeight      |
    UIViewAutoresizingFlexibleBottomMargin;
    [self setImageAtIndex:i toScrollView:imageScrollView];
}

- (void)setImageAtIndex:(NSInteger)index toScrollView:(ImageScrollView*)ScrollView
{
    UIWebView   *theWebView = (ScrollView.subviews)[0];
    if (index < 0 || [webViews count] <= index) {
        theWebView = nil;
        return;
    }

    dispatch_queue_t queueLoadPDF = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_sync(queueLoadPDF, ^{
        NSString    *document = [[[[NSString alloc]initWithString:_documwntPath]stringByAppendingString:@"/"]stringByAppendingFormat:@"Page%zd.pdf",(index + 1) ];

        NSURL   *url = [NSURL fileURLWithPath:document];
        NSString *urlString = [url absoluteString];
        NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *webURL = [NSURL URLWithString:encodedString];
        NSURLRequest    *req = [NSURLRequest requestWithURL:webURL];
        [theWebView loadRequest:req];
    });
}

The PDF pages are big. After calling loadRequest, program go back to the loop and creates new UIWebview.

I want to wait until webViewDidFinishLoad is called before returning to the loop.

Is there any solutions?

Thank you for your helps.

1

There are 1 best solutions below

3
On

@MasaruKitajima it's kinda hard to see what you are trying to do. My initial thoughts were as same as @Chinttu -Maddy- Ramani's but when I looked at you code again, I see more problem with my rough assumptions. I will throw couple of questions and you should think about your design again as you answer.

Somehow your code doesn't make sense to me. I see [webViews count] in your code. Do you have multiple webviews in a scroll view? But I see _webView which I think it's a member variable and you are initiating it in for-loop? what does that do?? You should be able to answer this first. You are probably loading only one webview not because you didn't know how to load the next webview in webViewDidFinishLoad but because you only have one webview??

Second, I think you want to load one webview at a time and that design doesn't make sense to me either. You should load the next webview when your ImageScrollView is scrolled down to the next webview. The idea is pretty much same as how you load the next data in table view when the last cell is displayed.