So, I make an rss reader for iphone, the code will be presented below, a question arose, the answer to which I couldn’t find in Google how to actually load the rss feed. Ie at this stage, the application loads and parses 20 posts from the habr, and, in fact, how can you further load the tape with its opening in the application. Thanks in advance for your help. The code itself is:
#import "ViewController.h" #import "TabelviewViewController.h" @interface ViewController () @end @implementation ViewController{ NSArray *tabs; NSArray *searchResults; NSDictionary *newsItem; } @synthesize tabelView; @synthesize webURL; //@synthesize rssURL; @synthesize urlx; @synthesize rssData; @synthesize news; @synthesize currentElement; @synthesize currentTitle; @synthesize pubDate; @synthesize urlPage; //@synthesize searchBar; - (void)viewDidLoad { [super viewDidLoad]; // создает массив в котором содержится СПИСОК // Do any additional setup after loading the view, typically from a nib. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; NSURL *url = [NSURL URLWithString:@"http://habrahabr.ru/rss/feed/posts/c8d94ef9646c8e98606bdb27fe465ca0/"]; NSURLRequest *theRequest=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { self.rssData = [NSMutableData data]; } else { NSLog(@"Connection failed"); } } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [rssData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *result = [[NSString alloc] initWithData:rssData encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); self.news = [NSMutableArray array]; NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:rssData]; rssParser.delegate = self; [rssParser parse]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@", error); } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { self.currentElement = elementName; if ([elementName isEqualToString:@"item"]) { self.currentTitle = [NSMutableString string]; self.pubDate = [NSMutableString string]; self.urlPage=[NSMutableString string]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if ([currentElement isEqualToString:@"title"]) { [currentTitle appendString:string]; } else if ([currentElement isEqualToString:@"pubDate"]) { [pubDate appendString:string];} else if ([currentElement isEqualToString:@"link"]) { [urlPage appendString:string]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"item"]) { NSDictionary *newsItem = [NSDictionary dictionaryWithObjectsAndKeys: currentTitle, @"title", pubDate, @"pubDate", urlPage,@"link", nil]; [news addObject:newsItem]; self.currentTitle = nil; self.pubDate = nil; self.currentElement = nil; self.urlPage = nil; } } - (void)parserDidEndDocument:(NSXMLParser *)parser { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; [self.tabelView reloadData]; } - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { NSLog(@"%@", parseError); } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [searchResults count]; } else{ return [news count]; } return [tabs count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; } newsItem = [news objectAtIndex:indexPath.row]; cell.textLabel.text = [newsItem objectForKey:@"title"]; cell.detailTextLabel.text = [newsItem objectForKey:@"pubDate"]; webURL= [NSURL URLWithString:[newsItem objectForKey:@"link"]]; newsItem=nil; return cell;}-(void)filterContentForSearchText:(NSString *)searchText scope:(NSString*)scope { NSPredicate *resultPridicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",searchText]; searchResults=[tabs filteredArrayUsingPredicate:resultPridicate]; } -(BOOL)searchDisplayController:(UISearchDisplayController *) controller shouldReloadTableForSearchString:(NSString *)searchString{ [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; return YES; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ indexPath=[self.tabelView indexPathForSelectedRow]; newsItem = [news objectAtIndex:indexPath.row]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ; } cell.textLabel.text = [newsItem objectForKey:@"title"]; cell.detailTextLabel.text = [newsItem objectForKey:@"pubDate"]; webURL= [NSURL URLWithString:[newsItem objectForKey:@"link"]]; [self performSegueWithIdentifier:@"showTabelDetail" sender:self]; } -(void)tapped { NSLog(@"TAPPED"); } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ NSMutableString *fURL = [newsItem objectForKey:@"link"]; //webURL = [NSURL URLWithString:fURL]; TabelviewViewController *dest = segue.destinationViewController; dest.url=fURL; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end