UIWebview
的shouldStartLoadWithRequest:navigationType
和webViewDidStartLoad
方法会在页面开始加载时被调用一次,但之后activityIndicator
会持续旋转(在webViewDidStartLoad
中开始),而webViewDidFinishLoad
和didFailLoadWithError
代理方法均不会被调用。该问题仅在IOS 6上出现。
UIWebview
的shouldStartLoadWithRequest:navigationType
和webViewDidStartLoad
方法会在页面开始加载时被调用一次,但之后activityIndicator
会持续旋转(在webViewDidStartLoad
中开始),而webViewDidFinishLoad
和didFailLoadWithError
代理方法均不会被调用。该问题仅在IOS 6上出现。
这在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