objective-c - iPad中的UIWebView问题

标签 objective-c ios uiwebview uiwebviewdelegate

我在尝试加载有效 URL 时看到以下消息并且没有网页:
delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:)等待 10 秒后未能返回。主运行循环模式:kCFRunLoopDefaultMode

任何人都可以帮助我吗?

使用:iOS 4.x 上的 iPad 2

评论:在 iOS 5.x 上似乎没问题

使用的代码:

    newsWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height-50.0-adHeight)];
    newsWebView.dataDetectorTypes = UIDataDetectorTypeAll;
    newsWebView.backgroundColor=[UIColor blackColor];
    [newsWebView setScalesPageToFit:YES];
    [self.view addSubview:newsWebView];
    newsWebView.delegate=self;
    [self webViewLoadRequest];


- (void)webViewLoadRequest 
{
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [newsWebView loadRequest:requestObj];
}

//Delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView {  

}

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    CGFloat scale=newsWebView.contentScaleFactor;
    NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = %f;",scale];
    [newsWebView stringByEvaluatingJavaScriptFromString:jsCommand];

    [rightButton setEnabled:YES];

    if(titleStatus==1){
        self.title=[newsWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
    }
} 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if (error.code == 101) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Open Page"
                                                        message:[error localizedDescription] delegate:nil
                                              cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];

    } else if (error.code == -1003) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Open Page"
                                                        message:@"Server cannot be found." delegate:nil
                                              cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];

    } else if (error.code == -1009) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Open Page"
                                                        message:@"No network connection." delegate:nil
                                              cancelButtonTitle:@"Dismiss"      otherButtonTitles:nil];
        [alert show];
        [alert release];

    } else if (error.code == -999) {
        // user probably stopped the web loading
    } else {
        NSLog(@"Error: %@", error.description);
    }
}

最佳答案

我没有看到你在任何地方设置 WebViews 委托(delegate)。如果确定在代码中创建 WebView,那么你应该设置它的委托(delegate)并实现它。

在设置 WebView 时添加以下行

     [newsWebViewNews setDelegate:self];

并实现< UIWebViewDelegate > 在类头文件中。

关于objective-c - iPad中的UIWebView问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11422885/

相关文章:

iOS 应用因 UIWebView 被拒绝

iphone - 如何在不让原始 View 消失的情况下将 modalViewController 覆盖在另一个 View 之上?

ios - NSPredicate 与 NSDate 按天/月过滤但不是年

ios - nskeyedarchiver 不适用于 ios8 但适用于 ios7 和模拟器

ios - Objective c 中整个游戏的计时器计数

ios - 在 View 中提交表单后如何关闭 UIWebView?

iphone - 从一个 UITable 控制另一个 UITable

ios - CALayer 不使用自动布局调整大小

ios - 将 UIWebView 内容保存为 A4 大小的 PDF

ios - Objective-C UIWebView 未加载网站