我有这组代码大约一个小时前运行良好。然后当我试图再次启动它几分钟后,UIWebView 只显示一个白屏。但是如果现在我屏蔽了 -(bool) 方法,就会出现 viewDidLoad。(用NSLog测试)请问代码发生了什么?它正在工作,但突然停止运行。
.m
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSString *titleString = @"Error Loading Page";
NSString *messageString = [error localizedDescription];
NSString *moreString = [error localizedFailureReason] ?
[error localizedFailureReason] :
NSLocalizedString(@"Try typing the URL again.", nil);
messageString = [NSString stringWithFormat:@"%@. %@", messageString, moreString];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titleString
message:messageString delegate:self
cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alertView show];
}
如果我实现上述方法,应用程序在打开 UIWebView 时会不断弹出错误域错误 -999 不停。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString];
NSRange range1= [currentURL rangeOfString:@"news"];
NSRange range2= [currentURL rangeOfString:@"pdf"];
if (range1.location == NSNotFound)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return YES;
}
if(range2.location==NSNotFound)
{
NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return YES;
}
return NO;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
webView.scalesPageToFit=YES;
webView.delegate = self;
NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html";
NSURL *url =[NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(@"sadfasdf");
}
.h @interface ViewController : UIViewController{ IBOutlet UIWebView*webView; }
@property(nonatomic,strong) UIWebView*webView;
@end