In my application I have a login page were the user can log in.
Once the user has logged-in to his account, he should go to his dashboard page(home page).
On the dashboard page there are three buttons add, edit and logout but in a dashboard page I call the URL to read the XML file from viewDidLoad
method before pressing any button.
I want to parse the XML file and save its value on the same page.
I have tried to parse the XML file on save page and I am using the value of the XML file on same page but I am not able to use that string value in another function on same page.
but I am not able to use the string user_login_xml
in the above method which is on the same page.. instead I get an error exc_bad_access
- (void)viewDidLoad
{
NSString *EditProfileID=Dataid;
NSString* result = [EditProfileID stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *urlAsString =[NSString stringWithFormat:@"http://www.mybusinesscentral.co.uk/mobile/iphone_profile_id.php?id="];
urlAsString=[urlAsString stringByAppendingString:result];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlAsString]];
urlCon = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (urlCon)
{
NSMutableData *mutableData = [[NSMutableData alloc] init];
self.receivedData=mutableData;
[mutableData release];
}
else //connection failed.
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"Error", @"Error")
message:NSLocalizedString(@"Error connecting to remote server", @"Error connecting to remote server")delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok") otherButtonTitles:nil];
[alert show];
[alert release];
}
[req release];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
[receivedData appendData:data1];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
self.receivedData = nil;
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:[NSString stringWithFormat:@"Connection failed! Error - %@ (URL: %@)", [error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *receivedDataAsString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
[receivedDataAsString release];
xmlParser = [[NSXMLParser alloc] initWithData:receivedData];
[xmlParser setDelegate:self];
[xmlParser parse];
[connection release];
self.receivedData = nil;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"details"])
{
NSString *user_login = [attributeDict objectForKey:@"user_login"];
user_login_xml = [NSString stringWithFormat:@"%@",user_login];
}
}
-(IBAction)Btn_AddNew:(id)sender
{
if ([user_login_xml length]==0)
{
AddNew *addnew = [[AddNew alloc]initWithNibName:@"AddNew" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:addnew animated:YES];
}
else
{
SubmitYourListing_Active *SubmitListing=[[SubmitYourListing_Active alloc] initWithNibName:@"SubmitYourListing_Active" bundle:[NSBundle mainBundle]];
SubmitListing.UserID=Dataid;
[self.navigationController pushViewController:SubmitListing animated:YES];
}
}
I am using TouchXMl, you can download from
https://github.com/TouchCode/TouchXML
and installation guide Click here
This is the easiest way to parse xml file, only few lines of code required.