I have a tag <AboutUs> (having HTML tags) and I have stored the string part in a local string. Now I want to pass it to my webView to display, but it is showing my string (null).
Here’s my code, any help is appreciated:
@class AppDelegate_iPhone;
@interface AboutUsViewController : UIViewController<NSXMLParserDelegate> {
NSMutableString *aboutUsString;
NSString *currentElement;
IBOutlet UIWebView *webView;
AppDelegate_iPhone *appDelegate;
}
@property(nonatomic,retain) NSMutableString *aboutUsString;
@property(nonatomic,retain) IBOutlet UIWebView *webView;
-(id)init;
@end
@implementation AboutUsViewController
@synthesize aboutUsString,webView;
-(id)init{
if(self == [super init]){
aboutUsString = [[NSMutableString alloc]init];
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
NSURL *url = [[NSURL alloc] initWithString:@"http://mobileecommerce.site247365.com/admin/AboutUs.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
AboutUsViewController *parser = [[AboutUsViewController alloc] init];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
NSLog(@"After Parsing=== = = = = = = = == = = = = %@",appDelegate.TextString);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = elementName;
if([currentElement isEqualToString:@"AboutUs"]) {
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentElement isEqualToString:@"AboutUs"]) {
NSMutableString *outputBuilder = [[NSMutableString alloc]init] ;
[outputBuilder appendString:[NSString stringWithFormat:@"%@", self.aboutUsString]];
[outputBuilder appendString:[NSString stringWithFormat:@"%@", string]];
self.aboutUsString = outputBuilder;
[outputBuilder release];
}
else
{
self.aboutUsString = string;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"AboutUs"]) {
[webView setScalesPageToFit:YES];
[webView loadHTMLString:self.aboutUsString baseURL:[NSURL URLWithString:@"http://www.hitchhiker.com/message"]];
NSLog(@"In DID End Element ===== %@",aboutUsString);
appDelegate.TextString = [[NSMutableString alloc]initWithString:aboutUsString];
}
currentElement = @"";
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"In Document End ========= %@",aboutUsString);
NSLog(@"Appdelgate Text String %@",appDelegate.TextString);
}
- (void)dealloc {
[super dealloc];
[webView release];
}
@end
In your
viewDidLoadmethod, you’re creating a new AboutUsViewController which then gets all theNSXMLParserdelegate calls. It has its own web view, that one will load the parsed HTML, but you’ll never see it because the parsing view controller is never actually visible.You should set
selfas the xml parser’sdelegateinstead of creating a new instance.