iphone web service access
Posted
by malleswar
on Stack Overflow
See other posts from Stack Overflow
or by malleswar
Published on 2010-04-14T12:39:17Z
Indexed on
2010/04/14
12:43 UTC
Read the original article
Hit count: 255
Hi,
I have .net webservice methods login and summary. After getting the result from login, I need to show second view and need to call summary method.
I am following this tutorial.
http://icodeblog.com/2008/11/03/iphone-programming-tutorial-intro-to-soap-web-services/
I created two new classes loginaccess.h and loginaccess.m.
@implementation LoginAccess
@synthesize ResultString,webData, soapResults, xmlParser;
-(NSString*)LoginCheck:(NSString*)userName:(NSString*)pwd {
NSString *soapMessage = [NSString stringWithFormat: @"\n" "\n" "\n" "\n" "%@" "%@" "" "\n" "\n",userName,pwd ];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://www.XXXXXXXXX.com/service.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [theRequest addValue: @"http://XXXXXXXXm/Login" forHTTPHeaderField:@"SOAPAction"]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection ) { webData = [[NSMutableData data] retain]; } else { NSLog(@"theConnection is NULL"); }
//[nameInput resignFirstResponder];
return ResultString;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength: 0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"ERROR with theConenction"); [connection release]; [webData release]; }
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"DONE. Received Bytes: %d", [webData length]); NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; NSLog(theXML); [theXML release];
if( xmlParser )
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[connection release];
[webData release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict {
if( [elementName isEqualToString:@"LoginResult"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
}
recordResults = TRUE;
}
} -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if( recordResults ) { [soapResults appendString: string]; } } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if( [elementName isEqualToString:@"LoginResult"]) { recordResults = FALSE;
ResultString = soapResults;
NSLog(@"Login");
[VariableStore setStr:ResultString];
NSLog(soapResults);
[soapResults release];
soapResults = nil;
}
}
@end
I am calling LoginCheck method and based on result I want to show the second view. Here after finishing of the button touch down event, it enter into did end element, so I am always getting nil value. If I use the same code controller it works fine as I push second view controller in didendelement.
Please give me some samples to place the web service calls in differnt class and how to call them in viewcontrollers.
Regards, Malleswar
© Stack Overflow or respective owner