我有这组代码,大约一个小时左右就能正常运行。然后,当我再次尝试再次启动它时,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
答案 0 :(得分:1)
加载请求时,委托方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
将被召唤。
在该方法中,您尝试无限次重新加载同一页面。
错误Domain = NSURLErrorDomain Code = -999“操作无法完成。(NSURLErrorDomain错误-999。)”UserInfo = 0xe203e80 {NSErrorFailingURLKey = http://www.imc.jhmi.edu/news.html,NSErrorFailingURLStringKey = http://www.imc.jhmi.edu/news.html}
我尝试了你的代码并得到了这个错误。如果在完成WebView的上一个请求之前发出另一个请求,则可能会发生此错误。因此,请尝试避免委托方法中重复加载请求的那些条件。
if(range2.location==NSNotFound)
这种情况永远是真的。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString];
NSRange range1= [currentURL rangeOfString:@"news"];
if (range1.location == NSNotFound)
{
NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
return YES;
}
return YES;
}
答案 1 :(得分:0)
UIWebView的委托方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
}
如果Web视图应该开始加载内容,则返回YES,否则返回NO。当您调用:
时,在viewDidLoad方法中调用该方法[webView loadRequest:requestObj];
在委托方法中,您再次调用相同的方法:
[webView loadRequest:[NSURLRequest requestWithURL:url]];
这转变为调用委托方法一次又一次地调用并形成无限循环。每次加载时都会发生这种情况,但在加载之前,会再次调用相同的loadRequest,形成循环。
在此方法中无需loadRequest,只需根据您所需的条件返回YES或NO。