Adding activity indicator on webview - objective-c

i m parsing an rss feed and loading in a webview...wat i want is ..to place a custom activity indicator in the exact place..where the parsing begins and the place where the parsing ends....below is the code.
#implementation MenuAndWineListViewController
NSDictionary *dict;
UIAlertView * errorAlert;
- (void)viewDidLoad
{
self.title=#"Menu & WineList";
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);
activityIndicator1.center=self.view.center;
[self.view addSubview:activityIndicator1];
NSURL *baseURL=[[NSURL
URLWithString:#"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if ([stories count] == 0) {
path = #"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&
sort_order=ASC&parent=180&child_of=180";
[self parseXMLFileAtURL:path];
}
[menuAndWineListViewController loadHTMLString:[dict objectForKey:#"description"] baseURL:nil];
[menuAndWineListViewController setClipsToBounds:YES];
menuAndWineListViewController.opaque=NO;
menuAndWineListViewController.backgroundColor=[UIColor clearColor];
[menuAndWineListViewController setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(#"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"])
{
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"item"])
{
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentSummary forKey:#"description"];
[stories addObject:[item copy]];
}
for (i=0 ; i<stories.count;i++)
{
dict = [stories objectAtIndex:i];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
else if ([currentElement isEqualToString:#"description"])
{
[currentSummary appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[activityIndicator1 stopAnimating];
[activityIndicator1 removeFromSuperview];
NSLog(#"stories array has %d items", [stories count]);
}

I'd put the
[activityIndicator1 startAnimating];
at the beginning of the parseXMLFileAtURL method
and the
[activityIndicator1 stopanimating];
at the beginning of the parserDidEndDocument like you did.

Related

Objective-C Pull to Refresh

I'm trying to implement a pull-to-refresh feature on my RSS feed table. The list pulls normally when loading the app, but I essentially need to replicate that for the pull-down.
Could anyone help as to why the code isn't working?
#import "RSSTableViewController.h"
#import "RSSDetailViewController.h"
#interface RSSTableViewController () {
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSString *element;
}
#end
#implementation RSSTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// RSS Settings
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://external.example.co.uk/newpost/example.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Pull to Refresh"];
[refresh addTarget:self
action:#selector(refreshView:)
forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
- (void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:#"Refreshing news feed..."];
// Refresh Logic
NSURL *url = [NSURL URLWithString:#"http://external.example.co.uk/newpost/example.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
// Set the timestamp
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:#"Last updated: %#",
[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
[self.tableView reloadData];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[refreshView endRefreshing]; //'Use of undeclared identifier 'refresh view'//
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the 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];
link = [[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:link 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"]) {
[link appendString:string];
}
}
-(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];
}
}
#end
Anyone got any ideas?
Code works fine
Needed to remove the line in parserDidEndDocument with the error.
I then had to add [feeds removeAllObjects] under the //Refresh Logic line to clear the current table contents.
Thanks guys.

Can't populate UITableView with data from XML (Russian text does not appear in slog like it should)

i can't figure out why i can't see a proper text when parsing XML page. My code is working but, when i try to output in NSLog, i can see following:
{
description = "\U0429\U0435\U043d\U043e\U043a \U0432 \U0434\U0430\U0440. \U041f\U0430\U043f\U0430 - \U043e\U0432\U0447\U0430\U0440 \U0412\U0415\U041e. \U041c\U0430\U043c\U0430 - \U043c\U0435\U0442\U0438\U0441 \U043b\U0430\U0431\U0440\U0430.";
}
I already did parse XML from example populated with English letters, so why i can't see a Russian text here? Or maybe it is not about the text and i made some mistakes?
There is a code from my NSObject class i use to get data from:
#implementation requestManager
-(id)initWithDelegate:(id<requestDelegate>)delegateObject{
self = [super init];
if (self){
self.delegate = delegateObject;
self.contentData = [NSMutableData data];
self.listOfItems = [NSMutableArray array];
}
return self;
}
-(void)loadXmlData{
NSURL *urlString = [NSURL URLWithString:#"http://*************url address**********"];
NSURLRequest *req = [NSURLRequest requestWithURL:urlString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.contentData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parserObj = [[NSXMLParser alloc]initWithData:self.contentData];
parserObj.delegate = self;
[parserObj parse];
}
#pragma mark - XML PARSER DELEGATE METHODS
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"description"]) isDescription = YES;
if ([elementName isEqualToString:#"text"]) isText = YES;
if ([elementName isEqualToString:#"name"]) isName = YES;
if ([elementName isEqualToString:#"images"]){
self.currentPosition = [NSMutableDictionary dictionary];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"description"]) isDescription = NO;
if ([elementName isEqualToString:#"text"]) isText = NO;
if ([elementName isEqualToString:#"name"]) isName = NO;
if ([elementName isEqualToString:#"images"]){
[self.listOfItems addObject:self.currentPosition];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(#"%#", self.listOfItems);
}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string{
if (isDescription) [self.currentPosition setValue:string forKey:#"description"];
if (isText) [self.currentPosition setValue:string forKey:#"text"];
if (isName) [self.currentPosition setValue:string forKey:#"name"];
}

Parsing works but data are not stored in nsMutableArray

I am parsing an xml file , it works and show me the data parsed in the console( for example : processing value for Speller ...), but they aren't added to the msmutablearray users. Here is some code. Where is the problem ? help please :
- (MyData *) initXMLParser {
[super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"user"]) {
NSLog(#"user element found – create a new instance of User class...");
user = [[User alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
}
NSLog(#"Processing value for : %#", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"user"]) {
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
}
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
} else {
[user setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
-(BOOL)parseDocumentWithData:(NSData *)data {
//NSString * filePath = [[NSBundle mainBundle] pathForResource:#"Users" ofType:#"xml"];
//data = [NSData dataWithContentsOfFile:filePath];
if (data == nil)
return NO;
// this is the parsing machine
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
// this class will handle the events
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:NO];
// now parse the document
BOOL ok = [xmlparser parse];
if (ok == NO)
NSLog(#"error");
else
NSLog(#"OK");
[xmlparser release];
return ok;
}
- (void) dealloc {
[currentElementValue release];
[super dealloc];
}
This seems to be the problematic part of your code
- (MyData *) initXMLParser {
[super init]; // change to self = [super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
You are not initializing self with the value of [super init];
And add [users release]; line to your dealloc call unless you release it elsewhere
change your didEndElement method with this:
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"user"]) {
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
}
}

UITable view,loading images from rss feed

I m reading the xml images from rss feed and parsing it in UITable view. Everything works fine, but it takes time to load the image content in the table view. The screen remains frozen. I'm using NSXMLParser to parse the image. Could you guys help me out, I'd be really greateful. Below is the code.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
//currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"url"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"content:encoded"]) {
[currentSummary appendString:string];
}
}
NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:#"url"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
cell.imageView.image=image1;
[image1 release];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines=2;
cell.textLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"title"];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"pubDate"];
Use Lazy Loading to load images....
somewhat dated but should put you in the right direction
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

UITable view,images,rssfeeds

i m using nsxmlparser to read the apple itunes rss feed..could u guys help to read this particular xml image.
<im:image height="55">http://a1.phobos.apple.com/us/r1000/028/Music/5c/aa/fe/mzi.fsnbyjmf.55x55-70.jpg</im:image>
below is the code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*) attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"href"])
{
currentString=[attributeDict objectForKey:#"href"];
NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"href"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"entry"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"published"];
//[item setObject:currentImage forKey:#"im:image height=55"];
NSLog(#"the current image content:%#",item);
[stories addObject:[item copy]];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"published"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"url"]) {
[currentContent appendString:string];
}
}
#user652878 try this coding its works well..... In didStartelement, write as follows.. else if([elementName isEqualToString:#"media:content"])
{
currentImage = [attributeDict valueForKey:#"url"];
}
In didEndElement, ....... else if([elementName isEqualToString:#"media:content"]){
[item setObject:currentImage forKey:#"image"];
}
In foundcharacters........................ else if([currentImage isEqualToString:#"media:content"])
{
[currentImage appendString:string];
}
use NSLog to see that we get image link or not.... Sure u will get ur solution...