IOS 6上UIWebview无法加载

3

UIWebviewshouldStartLoadWithRequest:navigationTypewebViewDidStartLoad方法会在页面开始加载时被调用一次,但之后activityIndicator会持续旋转(在webViewDidStartLoad中开始),而webViewDidFinishLoaddidFailLoadWithError代理方法均不会被调用。该问题仅在IOS 6上出现。


如果您展示代码,您可能会得到更多的帮助。否则,唯一的猜测是您没有设置UIWebViewDelegate。 - DenVog
1个回答

1

这在iOS 5和6上对我有效。确保在Interface Builder中连接您的插座。您应该进行某种检查以查看是否可用互联网访问。我是使用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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接