3

shouldStartLoadWithRequest:navigationTypewebViewDidStartLoad方法UIWebview被调用一次,但之后activityIndicator继续旋转(从 开始webViewDidStartLoad)并且没有任何委托方法webViewDidFinishLoad或被didFailLoadWithError调用。这只是 IOS 6 上的问题。

4

1 回答 1

1

这在 iOS 5 和 6 中对我有用。确保在 Interface Builder 中连接插座。您可能应该做一些检查,看看是否可以访问 Internet。我正在使用 Apple Reachability 课程:

标题:

#import <UIKit/UIKit.h>
@interface HelpWebViewController : UIViewController <UIWebViewDelegate>
{
    IBOutlet UIWebView *webView;
    IBOutlet UIActivityIndicatorView *activityIndicator;
}

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, strong) NSString *webHelpURLString;

- (void)webViewDidStartLoad:(UIWebView *)webView; //a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; //web view failed to load

@end

实现文件:

 #import "HelpWebViewController.h"
    #import "Reachability.h" // Needs System Configuration Framework

    @interface HelpWebViewController ()

    @end

    @implementation HelpWebViewController

    @synthesize webView = ivWebView;
    @synthesize activityIndicator = ivActivityIndicator;
    @synthesize webHelpURLString = ivWebHelpURLString;

    -(BOOL)reachable {
        Reachability *r = [Reachability reachabilityWithHostName:@"apple.com"];
        NetworkStatus internetStatus = [r currentReachabilityStatus];
        if(internetStatus == NotReachable) {
            return NO;
        }
        return YES;
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];    

        NSURL *webHelpURL = [NSURL URLWithString:@"support.apple.com"];
        NSURLRequest *myrequest = [NSURLRequest requestWithURL:webHelpURL];
        [self.webView loadRequest:myrequest];
        self.webView.scalesPageToFit = YES;

        if ([self reachable]) {
            NSLog(@"Reachable");
        }
        else {
            NSLog(@"Not Reachable");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"cantOpenWebPageAlertTitle", @"Cannot Open Page - UIAlert can't open web page")
                                                            message:NSLocalizedString(@"cantOpenWebPageAlertMessage", @"The page cannot be opened because you're not connected to the Internet.")
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"cantOpenWebPageAlertOKButton", @"OK - accept alert")
                                                  otherButtonTitles:nil];
            [alert show];
            alert = nil;
        }
    }

    - (void)webViewDidStartLoad:(UIWebView *)webView
    {

       [self.activityIndicator startAnimating];
 }

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [self.activityIndicator stopAnimating];
    }

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        [self.activityIndicator stopAnimating];
    }

    @end
于 2012-12-16T16:49:24.640 回答