0

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.

Masaru Kitajima
  • 343
  • 1
  • 2
  • 13
  • why don't you call - (void)setImageAtIndex:(NSInteger)index toScrollView:(ImageScrollView*)ScrollView in webViewDidFinishLoad? – HMHero Nov 11 '14 at 04:54
  • @Masaru Kitajima i think you should integrate webview delegate and call `[self setImageAtIndex:i toScrollView:imageScrollView];` this method from `webViewDidFinishLoad:` method.take `i` as global and increase it at every `webViewDidFinishLoad`.i think this will work.do not use for loop. – ChintaN -Maddy- Ramani Nov 11 '14 at 05:02
  • I'm not quite sure what you kindly mentioned. (void)setImageAtIndex:(NSInteger)index toScrollView:(ImageScrollView*)ScrollView is to load PDF into UIWebView and I want to wait until PDF is loaded. – Masaru Kitajima Nov 11 '14 at 05:05
  • Chinttu, thanks for your comment. As kindly @HMHERO mentioned, you both says put [self setImageAtIndex:i toScrollView:imageScrollView]; in webViewDidFinishLoad. I've thought that webViewDidFinshLoad is called that UIWebView successfully load the data. Is this incorrect? And you said not to use for loop. But I have several PDFs to load, so how can I do that without for loop? Using while loop is OK? – Masaru Kitajima Nov 11 '14 at 05:35

1 Answers1

0

@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.

HMHero
  • 2,333
  • 19
  • 11
  • thank you for your comment. I create several UIWebviews in the for-loop and "webViews" is a NSMutableArray and each time I create a UIWebview, I do [webViews addObject:_webView]; to store each UIWebView. For the question 2, I want to load one UIWebView at a time and once loading is completed, I guess webViewDidFinishLoad. Then I want to proceed to the next UIWebView. – Masaru Kitajima Nov 11 '14 at 07:58
  • You are reinitiating _webView in every for-loop cycle. Where you run [webViews addObject:_webView];? – HMHero Nov 11 '14 at 08:58
  • I thought I create a new _webView instance in every for-ioop. I run [webViews addObject:_webView]; right after [self setImageAtIndex:i toScrollView:imageScrollView];. – Masaru Kitajima Nov 11 '14 at 09:02