WCF + Objective-C
Autor: Daniel Colnaghi | Publicado: 2011-04-25T16:30:00-03:00

Depois de muito tentar e procurar uma alternativa para fazer a conexão de um serviço WCF com minha aplicação, finalmente achei uma solução.

Bom primeiro devemos adicionar as variaveis no header da classe que irá conter a conexão.


NSMutableData *webData; NSMutableString *soapResults; NSURLConnection *conn;
NSXMLParser *xmlParser; BOOL elementFound;

Depois na implementação o método que irá chamar o serviço.

NSString *SOAPEnvelope = [NSString stringWithFormat:@"&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n"
"&lt;SOAP-ENV:Envelope \n"
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
"xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt; \n"
"&lt;SOAP-ENV:Body&gt; \n"
"&lt;HelloWorld xmlns=\"http://tempuri.org/\"&gt;"
"&lt;/HelloWorld&gt; \n"
"&lt;/SOAP-ENV:Body&gt; \n"
"&lt;/SOAP-ENV:Envelope&gt;"];
//DebugNSLog(@"SOAP ENVELOPE %@", SOAPEnvelope); 
NSURL *url = [NSURL URLWithString:@"http://SERVER/WCFServiceName/ServiceName.svc"];     &nbsp;NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//Header     &nbsp;NSString *SOAPEnvelopeLength = [NSString stringWithFormat:@"%d", [SOAPEnvelope length]];     &nbsp;[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];     &nbsp;[request addValue:@"http://tempuri.org/WCFServiceName/HelloWorld" forHTTPHeaderField:@"SOAPAction"];[request addValue:SOAPEnvelopeLength forHTTPHeaderField:@"Content-Length"];
//Method &amp; Body[request setHTTPMethod:@"POST"];     &nbsp;[request setHTTPBody:[SOAPEnvelope dataUsingEncoding:NSUTF8StringEncoding]];
// Connectionconn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {      &nbsp;webData = [[NSMutableData data] retain];      &nbsp;}
E depois adicione os métodos de comunicação com o serviço na mesma classe.

-(void) connection:(NSURLConnection *) connectiondidReceiveResponse:(NSURLResponse *) response {&nbsp; &nbsp; [webData setLength: 0];}
-(void) connection:(NSURLConnection *) connection&nbsp; &nbsp; didReceiveData:(NSData *) data {&nbsp; &nbsp; [webData appendData:data];}
-(void) connection:(NSURLConnection *) connection&nbsp; didFailWithError:(NSError *) error {&nbsp; &nbsp; [webData release];&nbsp; &nbsp; [connection release];}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {&nbsp; &nbsp; NSLog(@"DONE. Received Bytes: %d", [webData length]);&nbsp; &nbsp; NSString *theXML = [[NSString alloc]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;initWithBytes: [webData mutableBytes]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;length:[webData length]&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;encoding:NSUTF8StringEncoding]; &nbsp; &nbsp; //Debug&nbsp; &nbsp; NSLog(@"XML %@",theXML); &nbsp; &nbsp; [theXML release];
 if (xmlParser) {  &nbsp; &nbsp; &nbsp; &nbsp; [xmlParser release];  &nbsp; &nbsp; }  &nbsp; &nbsp; xmlParser = [[NSXMLParser alloc] initWithData: webData]; &nbsp; &nbsp; [xmlParser setDelegate:self]; &nbsp; &nbsp; [xmlParser setShouldResolveExternalEntities:YES]; &nbsp; &nbsp; [xmlParser parse];  &nbsp; &nbsp; [connection release]; &nbsp; &nbsp; [webData release]; }
-(void) parser:(NSXMLParser *) parserdidStartElement:(NSString *) elementName&nbsp; namespaceURI:(NSString *) namespaceURI&nbsp;qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {   //Debug NSLog(@"%@", elementName); &nbsp; &nbsp; if( [elementName isEqualToString:@"HelloWorldResult"]) {  &nbsp; &nbsp; &nbsp; &nbsp; if (!soapResults) {   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; soapResults = [[NSMutableString alloc] init];   &nbsp; &nbsp; &nbsp; &nbsp; }  &nbsp; &nbsp; &nbsp; &nbsp; elementFound = YES;  &nbsp; &nbsp; } }
-(void)parser:(NSXMLParser *) parser&nbsp;foundCharacters:(NSString *)string { &nbsp; &nbsp; if (elementFound) {&nbsp; &nbsp; &nbsp; &nbsp; [soapResults appendString: string];&nbsp; &nbsp; }}
-(void)parser:(NSXMLParser *)parserdidEndElement:(NSString *)elementName&nbsp;namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName { &nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"SOAP RESULT %@", soapResults); &nbsp; &nbsp; if ([elementName isEqualToString:@"HelloWorldResult"]) {  
&nbsp; &nbsp; &nbsp; &nbsp; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;message:soapResults       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;delegate:self        &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;cancelButtonTitle:@"OK" &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;otherButtonTitles:nil];  &nbsp; &nbsp; &nbsp; &nbsp; [alert show];  &nbsp; &nbsp; &nbsp; &nbsp; [alert release];  &nbsp; &nbsp; &nbsp; &nbsp; [soapResults setString:@""];  &nbsp; &nbsp; &nbsp; &nbsp; elementFound = FALSE;  &nbsp; &nbsp; } }