
Consuming a web SOAP web service (asmx) with iphone (sdk)
here i go
View controller
//
// Hello_SOAPAppDelegate.m
// Hello_SOAP
//
// Created by Dave McAnall on 11/2/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
#import "Hello_SOAPViewController.h"
@implementation Hello_SOAPViewController
@synthesize greeting, tonumber,fromnumber,nameInput, webData, soapResults, xmlParser;
-(IBAction)buttonClick:(id)sender
{
recordResults = FALSE;
NSString *soapMessage = [NSString stringWithFormat:
@"\n"
"
"
"
"
"
"
"
"
"
"
];
NSLog(soapMessage);
NSURL *url = [NSURL URLWithString:@"http://yourasmx.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://tempuri.org/SendTextToFriend" 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];
[fromnumber resignFirstResponder];
[tonumber resignFirstResponder];
}
-(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:@"SendTextToFriendResult"])
{
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:@"SendTextToFriendResult"])
{
recordResults = FALSE;
greeting.text = soapResults;
[soapResults release];
soapResults = nil;
}
}
/*
Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
*/
/*
Implement viewDidLoad if you need to do additional setup after loading the view.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc
{
[xmlParser release];
[super dealloc];
}
@end
Header
//
// Hello_SOAPViewController.h
// Hello_SOAP
//
// Created by Dave McAnall on 11/2/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
#import
@interface Hello_SOAPViewController : UIViewController
{
IBOutlet UITextField *nameInput;
IBOutlet UITextField *tonumber;
IBOutlet UITextField *fromnumber;
IBOutlet UILabel *greeting;
NSMutableData *webData;
NSMutableString *soapResults;
NSXMLParser *xmlParser;
BOOL *recordResults;
}
@property(nonatomic, retain) IBOutlet UITextField *nameInput;
@property(nonatomic, retain) IBOutlet UITextField *tonumber;
@property(nonatomic, retain) IBOutlet UITextField *fromnumber;
@property(nonatomic, retain) IBOutlet UILabel *greeting;
@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableString *soapResults;
@property(nonatomic, retain) NSXMLParser *xmlParser;
-(IBAction)buttonClick: (id) sender;
@end
XIB
2 comments:
Hi Dave,
Thank you for the great sample code(HelloSoap).
I would like to download a pdf file by calling web services, can you please provide me some hints how to handle this in your sample code?
P.S use iOS + Soap
Thanks again,
Nima
How to write tow times in same viewController?
Post a Comment