【问题标题】:UIWebView not loading dataUIWebView 不加载数据
【发布时间】:2015-08-17 05:06:42
【问题描述】:

我正在尝试在 iOS 8 上的 UIWebView 中加载网页。委托方法 - webViewDidStartLoadwebViewDidFinishLoad 被调用,但页面仍然是白色的。

我要加载的网址是:

http://us.rd.yahoo.com/finance/news/rss/story/
http://finance.yahoo.com/news/apple-test-australian-corporate-bond-035643114.html

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURLRequest *newsUrlRequest = [[NSURLRequest alloc] initWithURL:self.newsArticleURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:newsUrlRequest queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if ([data length] > 0 && error == nil) {
            [self.newsWebView loadRequest:newsUrlRequest];
        }
    }];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [self.pageLoadingSpinnger startAnimating];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.pageLoadingSpinnger stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Oops" message:@"The page you requested could not be loaded.\nPlease go back and try again." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
    [errorAlertView show];
}

【问题讨论】:

  • 为什么要使用NSOperationQueue?可以在创建NSURLRequest *newsUrlRequest之后直接做[self.newsWebView loadRequest:newsUrlRequest];

标签: ios uiwebview


【解决方案1】:

我已经尝试过,这对我来说非常适合。

NSString *strURL = @"http://us.rd.yahoo.com/finance/news/rss/story/*http://finance.yahoo.com/news/apple-test-australian-corporate-bond-035643114.html";
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:strURL]];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[myWebView loadRequest:request];

【讨论】:

    【解决方案2】:
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        NSLog(@"Failed to load with error :%@",[error debugDescription]);
    }
    

    尝试此代码来识别您的错误,然后您就可以轻松解决它。

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2012-01-13
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多