Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have written soap_parser Class to get read of making SOAP Message, its pretty straight forward to implement, just follow the steps and your through the problem.</p> <p>Step 1: Create a .h file <code>soap_parser.h</code> with following code and make appropiate domain changes</p> <pre><code>#error Set Your Request Domain &amp; Webservice name #define DOMAIN_URL @"http://yourDomain.com/WebService/" #define SERVICE_URL DOMAIN_URL@"/iphoneservice.asmx" #define TEMP_URL @"http://tempuri.org" #import &lt;Foundation/Foundation.h&gt; @protocol soap_parser_delegate &lt;NSObject&gt; @optional -(void)receivedResponseWithStatusCode:(NSInteger)statusCode; -(void)requestFailedWithError:(NSError *)err; @required -(void)dataReceivingCompleted:(NSMutableData *)data; @end @interface soap_parser : NSObject { NSMutableURLRequest *soap_request; NSURLResponse *soap_response; NSMutableData *soap_responseData; NSString *currentAction; id &lt;soap_parser_delegate&gt;delegate; } @property (nonatomic, retain) NSMutableData *soap_responseData; @property (nonatomic, retain) NSString *currentAction; @property (nonatomic, retain) id &lt;soap_parser_delegate&gt;delegate; #pragma mark - Initialize Parsing -(void)startParsingWithAction:(NSString *)action andWithParams:(NSDictionary *)params; #pragma mark - Create SOAP message -(NSString *)createSoapMesssageFrom:(NSDictionary *)requestParam; @end </code></pre> <p>Step 2: Create a .m file <code>soap_parser.m</code> with following code</p> <pre><code>#import "soap_parser.h" @implementation soap_parser @synthesize soap_responseData, currentAction, delegate; #pragma mark - Initialize Parsing -(void)startParsingWithAction:(NSString *)action andWithParams:(NSDictionary *)params { self.currentAction = action; NSString *reqSOAPmsg = [self createSoapMesssageFrom:params]; NSURL *url = [NSURL URLWithString:SERVICE_URL]; soap_request = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [reqSOAPmsg length]]; [soap_request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [soap_request addValue: [NSString stringWithFormat:@"%@/%@",TEMP_URL,self.currentAction] forHTTPHeaderField:@"SOAPAction"]; [soap_request addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [soap_request setHTTPMethod:@"POST"]; [soap_request setHTTPBody: [reqSOAPmsg dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *cn = [[NSURLConnection alloc] initWithRequest:soap_request delegate:self]; [cn start]; } #pragma mark - Create SOAP message -(NSString *)createSoapMesssageFrom:(NSDictionary *)requestParam { NSMutableString *soapMessage = [[NSMutableString alloc] init]; [soapMessage appendFormat:@"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n" "&lt;soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;\n" "&lt;soap:Body&gt;\n" "&lt;%@ xmlns=\"http://tempuri.org/\"&gt;\n",self.currentAction]; for(NSString *key in requestParam) { [soapMessage appendFormat:@"&lt;%@&gt;%@&lt;/%@&gt;\n",key,[requestParam valueForKey:key],key]; } [soapMessage appendFormat:@"&lt;/%@&gt;\n" "&lt;/soap:Body&gt;\n" "&lt;/soap:Envelope&gt;",self.currentAction]; NSLog(@"%@",soapMessage); return soapMessage; } #pragma mark - NSURLConnection Delegate -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; if([delegate respondsToSelector:@selector(receivedResponseWithStatusCode:)]) { [delegate performSelector:@selector(receivedResponseWithStatusCode:) withObject:httpResponse]; } self.soap_responseData = [[NSMutableData alloc] initWithCapacity:0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.soap_responseData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { if([delegate respondsToSelector:@selector(requestFailedWithError:)]) { [delegate performSelector:@selector(requestFailedWithError:) withObject:error]; } connection = nil; self.soap_responseData = nil; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { if([delegate respondsToSelector:@selector(dataReceivingCompleted:)]) { [delegate performSelector:@selector(dataReceivingCompleted:) withObject:self.soap_responseData]; } } @end </code></pre> <p>Step 3: <code>#import "soap_parser.h"</code> and <code>&lt;soap_parser_delegate&gt;</code> in your <code>ViewController</code> .h file, where you want to make soap Call.</p> <p>Step 4: Implement Following Delegate methods in your .m file</p> <pre><code>#pragma mark - SOAP parser Delegate -(void)receivedResponseWithStatusCode:(NSInteger)statusCode { NSLog(@"Status Code : %d",statusCode); } -(void)requestFailedWithError:(NSError *)err { NSLog(@"Failed : %@",[err description]); } -(void)dataReceivingCompleted:(NSMutableData *)data { NSLog(@"Response Data Length : %d",data.length); NSString *responseString = [[NSString alloc] initWithBytes:[data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",responseString); #warning Do something with your Response Data } </code></pre> <p>Step 5: Create A Dictionary with Data to send in SOAP Request to Make a call to your SOAP_WEBSERVICE, and rest it will take care.</p> <pre><code>NSDictionary *dictParams = [NSDictionary dictionaryWithObjectsAndKeys:@"CUST ID", @"cust", @"Transaction ID",@"tran", @"Ret Value",@"ret", @"PPAY Value",@"ppay", @"RECP Value",@"recp", @"SCODE Value",@"sCode", @"MY COMPANY LTD.",@"companyShortName", @"COMPANY_007",@"companyCode", nil]; // Create Object of SOAP_Parser soap_parser *objSOAP = [[soap_parser alloc] init]; // Set Delegate to self [objSOAP setDelegate:self]; // Send Request with param Dictionary [objSOAP startParsingWithAction:@"UserLogin" andWithParams:dictParams]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload