Consuming WCF Web Services in an iOS Application

Consuming WCF Web Services in an iOS Application
-(void)viewDidLoad {

[super viewDidLoad];

//Web Service Call
NSString *soapMessage = [NSString stringWithFormat:
@"\n"
"; \n"
"\n"
""
" \n"
" \n"
""];

//[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURL *url = [NSURL URLWithString:@"http://[IP_ADDRESS_OF_WCF_SERVER]/IphoneService/Service1.svc"]; 
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/IService1/GetMembers"; 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");
}
}

- (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 {
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];

NSLog(@"Data has been loaded");

NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
[webData release];

label.text = responseString;
NSLog(responseString);
[responseString release]; 
}

- (void)dealloc {
[super dealloc];
}
Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading