3

これはQ&Aの投稿です。StackOverflowのデータベースを閲覧しているときに、この問題に苦しんでいる他の多くのユーザーに通知します。これらのユーザーの誰も確かな答えを得たことはありません(そして彼らのほとんどは何年も前に質問を提起していたので、私は投稿をぶつけるつもりはありませんでした)。


多くの人が苦労している問題は次のとおりです。

UIWebViewにページを読み込もうとして、そのページがiFrameを介して別のページを読み込むか、JavaScriptを介して広告を読み込むと、ページ読み込み関数が再度呼び出され、UIActivityIndi​​catorを使用している場合は再度呼び出されます。同様に、ユーザーを苛立たせます。

これに対する修正は、以前のMainURLを保存し、ロード機能を無視するよりも一致する場合は、ロードしようとしている新しいMainURLをチェックすることです。(以下の私の答えのサンプルコード)

** Webページが実際webViewDidStartLoadにロードされた後にメソッドを追加で呼び出すWebページの例は、次のようになります:www.speakeasy.net/speedtest/

4

1 に答える 1

5
//Define the NSStrings "lastURL" & "currentURL" in the .h file.
//Define the int "falsepositive" in the .h file. (You could use booleans if you want)
//Define your UIWebView's delegate (either in the xib file or in your code `<UIWebViewDelegate>` in .h and `webView.delegate = self;` in .m viewDidLoad)

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    lastURL = [NSString stringWithFormat:@"%@", webView.request.mainDocumentURL];
    if (falsepositive != 1) {
        NSLog(@"Loaded");
        //hide UIActivityIndicator
    } else {
        NSLog(@"Extra content junk (i.e. advertisements) that the page loaded with javascript has finished loading");
        //This method may be a good way to prevent ads from loading hehe, but we won't do that
    }
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
    NSURL *requestURL = [request mainDocumentURL];
    currentURL = [NSString stringWithFormat:@"%@", requestURL]; //not sure if "%@" should be used for an NSURL but it worked..., could cast `(NSString *)` if we *really* wanted to...
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    if ([currentURL isEqualToString:lastURL]) {
        falsepositive = 1;
        NSLog(@"The page is loading extra content with javascript or something, ignore this");
    } else {
        falsepositive = 0;
        NSLog(@"Loading");
        //show UIActiviyIndicator
    }
}

//make sure you read the //comments// at the top of this code snippet so that you properly define your .h variables O:) Thanks!



//
于 2012-11-20T04:31:49.400 に答える