【发布时间】:2015-08-17 05:06:42
【问题描述】:
我正在尝试在 iOS 8 上的 UIWebView 中加载网页。委托方法 - webViewDidStartLoad 和 webViewDidFinishLoad 被调用,但页面仍然是白色的。
我要加载的网址是:
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];。