gpt4 book ai didi

ios - rss 提要中的 WebView 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 12:16:38 26 4
gpt4 key购买 nike

您好,我正在 Xcode 中创建 Rss Feeder,但我的 Web View 无法正常工作 为什么不工作?

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 仍然是白色

最佳答案

终于开工了!不要忘记在之前创建 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];
}

关于ios - rss 提要中的 WebView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31824854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com