rss 提要中的 WebView 无法正常工作
WebView in rss feed not working
您好,我正在 Xcode 中创建 Rss Feeder,但我的网络视图无法正常工作
为什么不起作用?
APPMasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url1 = [NSURL URLWithString:@"http://raphaelsebban.com/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url1];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
url = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:url forKey:@"link"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"link"]) {
[url stringByAppendingString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
[[segue destinationViewController] setUrl:string];
}
}
APPDetailViewController
- (void)viewDidLoad {
[ super viewDidLoad];
NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF16StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
[self webViewDidFinishLoad:_webView];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"method was called");
}
这是我的日志:
>2015-08-05 09:10:51.810 #raph[4226:436663] method was called
它似乎不起作用,我的 webView 仍然是白色
我有同样的错误。有些 URL 已打开,但有些只显示白屏。所以我做了这个改变,现在总是 webview load url.
这是我的代码:
-(void)addWebView {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width, self.view.frame.size.height)];
webView.backgroundColor = [UIColor clearColor];
webView.delegate = self;
[self.view addSubview:webView];
[webView addSubview:self.actInd];
//self.linkURL is some URL NSString (not NSURL)
NSString *htmlStr = [NSString stringWithFormat:@"<script>window.location=%@;</script>",[[NSString alloc]
initWithData:[NSJSONSerialization dataWithJSONObject:self.linkURL options:NSJSONReadingAllowFragments error:NULL] encoding:NSUTF8StringEncoding]];
[webView loadHTMLString:htmlStr baseURL:nil];
webView.scalesPageToFit = YES;
}
希望这对您有所帮助并解决您的问题
您是否使用了“http://chicago.craigslist.org/search/ata?format=rss'? or 'http://chicago.craigslist.org/search/ata?format=rss&format=rss”?您可能必须使用第二个,因为它指定了类别。
并检查您的 rss 是否有问题或不明白。
终于成功了!不要忘记在 .
之前创建 IBOutlet
祝你好运!
这是我的代码:
APPMasterViewcontroller
- (void)viewDidLoad {
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *rssUrl = [NSURL URLWithString:@"yourlink/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:rssUrl];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
url = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:url forKey:@"link"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"link"]) {
[url appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
[[segue destinationViewController] setUrl:string];
//}
}
**APPDetailViewController**
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF16StringEncoding]];
NSString *urlString=[self.url stringByReplacingOccurrencesOfString:@"\n\t\t" withString:@""];
myURL =[NSURL URLWithString:urlString];
NSLog(@"url=%@",urlString);
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}
您好,我正在 Xcode 中创建 Rss Feeder,但我的网络视图无法正常工作 为什么不起作用?
APPMasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url1 = [NSURL URLWithString:@"http://raphaelsebban.com/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url1];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
url = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:url forKey:@"link"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"link"]) {
[url stringByAppendingString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
[[segue destinationViewController] setUrl:string];
}
}
APPDetailViewController
- (void)viewDidLoad {
[ super viewDidLoad];
NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF16StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
[self webViewDidFinishLoad:_webView];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"method was called");
}
这是我的日志:
>2015-08-05 09:10:51.810 #raph[4226:436663] method was called
它似乎不起作用,我的 webView 仍然是白色
我有同样的错误。有些 URL 已打开,但有些只显示白屏。所以我做了这个改变,现在总是 webview load url.
这是我的代码:
-(void)addWebView {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width, self.view.frame.size.height)];
webView.backgroundColor = [UIColor clearColor];
webView.delegate = self;
[self.view addSubview:webView];
[webView addSubview:self.actInd];
//self.linkURL is some URL NSString (not NSURL)
NSString *htmlStr = [NSString stringWithFormat:@"<script>window.location=%@;</script>",[[NSString alloc]
initWithData:[NSJSONSerialization dataWithJSONObject:self.linkURL options:NSJSONReadingAllowFragments error:NULL] encoding:NSUTF8StringEncoding]];
[webView loadHTMLString:htmlStr baseURL:nil];
webView.scalesPageToFit = YES;
}
希望这对您有所帮助并解决您的问题
您是否使用了“http://chicago.craigslist.org/search/ata?format=rss'? or 'http://chicago.craigslist.org/search/ata?format=rss&format=rss”?您可能必须使用第二个,因为它指定了类别。
并检查您的 rss 是否有问题或不明白。
终于成功了!不要忘记在 .
之前创建 IBOutlet祝你好运!
这是我的代码:
APPMasterViewcontroller
- (void)viewDidLoad {
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *rssUrl = [NSURL URLWithString:@"yourlink/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:rssUrl];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
url = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:url forKey:@"link"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"link"]) {
[url appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
[[segue destinationViewController] setUrl:string];
//}
}
**APPDetailViewController**
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString: [self.url stringByAddingPercentEscapesUsingEncoding: NSUTF16StringEncoding]];
NSString *urlString=[self.url stringByReplacingOccurrencesOfString:@"\n\t\t" withString:@""];
myURL =[NSURL URLWithString:urlString];
NSLog(@"url=%@",urlString);
NSURLRequest *request = [NSURLRequest requestWithURL:myURL];
[self.webView loadRequest:request];
}