objective-c - UIWebView 模拟器突然不工作了

标签 objective-c ios uiwebview

我有这组代码,大约一个小时前运行良好。然后,当我在几分钟前再次尝试启动它时,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

最佳答案

当你加载一个请求时,委托(delegate)方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

会被调用。

在该方法中,您尝试无限次地重新加载同一页面。

Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0xe203e80 {NSErrorFailingURLKey=http://www.imc.jhmi.edu/news.html, NSErrorFailingURLStringKey=http://www.imc.jhmi.edu/news.html}

我尝试了您的代码并遇到了这个错误。如果在 WebView 的上一个请求完成之前进行另一个请求,则可能会出现此错误。所以尽量避免在委托(delegate)方法中重复加载请求的那些情况。

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;
}

关于objective-c - UIWebView 模拟器突然不工作了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237814/

相关文章:

iphone - 如何在 NSNumber 上做数学运算

html - 如何删除移动 Twitter 闪屏?

objective-c - 在ios中创建对象的最简单方法

ios - 我无法在 MagicalRecord 中找到 FirstByAttribute

objective-c - 单击UINavigationBar按钮时的光泽效果

objective-c - 以编程方式添加导出类型 UTI 以进行应用内购买

ios - 插入行后重新加载 tableView

ios - 如何在 UIWebView Video Player 中添加 OverLay View?

swift - 我的 webView 占用了太多内存吗?

ios - 在 iOS 中清除 safari 缓存