SoFunction
Updated on 2025-04-09

How to call WebService (SOAP interface) with IOS

During a project development process, IOS is used to call the WebService interface, so I took some time to sort out the content in this area and share it with you.

Method 1: Use the WSDL2ObjC tool to convert the interface to an OC class.

1. Enter the webService interface address in the browser (Safari is not available, I am using Firefox), such as:, add .wsdl to the address after itOpen.

2. Save the page as a wsdl file, add .wsdl to the suffix when saving, and save it as such.

3. Use the WSDL2ObjC tool to convert the wsdl file into an OC class.

4. Import all files of the generated OC class into the project. Then it can be called. Now that I haven't written a demo, I will post some of the calling codes and I will pass on a demo in the future.

- (NSString *)skHkshListOfpagenow2:(NSInteger)aPagenow pagesize:(NSInteger)aPagesize {
   
  MURPXzshServiceSoapBinding *binding = [[MURPXzshServiceSoapBinding alloc]initWithAddress:[NSString stringWithFormat:@"%@%@", , XZSH_SERVICE]];//Interface address   = YES;//Can directly copy  MURPXzshService_HkshList *parm = [[MURPXzshService_HkshList alloc] init];//Initialization of method in interface   = [umcid stringValue];//Pass parameters in the interface   = [NSNumber numberWithInteger:aPagenow];//Pass parameters in the interface   = [NSNumber numberWithInteger:aPagesize];//Pass parameters in the interface  MURPXzshServiceSoapBindingResponse *resp = [binding HkshListUsingParameters:parm];//Calling method   
  //The following is the return value  for (id mine in ) {
   
    if ([mine isKindOfClass:[MURPXzshService_HkshListResponse class]]) {
       
      NSString *resultStr = [mine HkshListResult];
       
      return resultStr;
    }
  }
  return nil;
}

Note: The name of my webService above is MURPXzshService, Soap is MURPXzshServiceSoapBinding, and the method called is HkshList.

Method 2: Directly request the call through the network to parse XML method.

Let's not talk about it, first upload the code:

- (void)viewDidAppear:(BOOL)animated {
   
  [super viewDidAppear:animated];
   
  //If you start the request here  NSString *webServiceBodyStr = [NSString stringWithFormat:
                  @"<Jsjy_yjy xmlns=\"/murpwebservice/\">"
                  "<xxx>34192</xxx>"
                  "<pagenow>1</pagenow>"
                  "<pagesize>20</pagesize>"
                  "</Jsjy_yjy>"];// Here are the parameters  NSString *webServiceStr = [NSString stringWithFormat:
                @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                "<soap:Envelope xmlns:xsi=\"http:///2001/XMLSchema-instance\" xmlns:xsd=\"http:///2001/XMLSchema\" xmlns:soap=\"/soap/envelope/\">\n"
                "<soap:Body>\n"
                "%@\n"
                "</soap:Body>\n"
                "</soap:Envelope>",
                webServiceBodyStr];//webService header   
  NSString *SOAPActionStr = [NSString stringWithFormat:@"/murpwebservice/%@", @"Jsjy_yjy"];//SOAPAction
   
  NSURL *url = [NSURL URLWithString:@"http://xxxx/key/"];
  NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
  NSString *msgLength = [NSString stringWithFormat:@"%ld", ];
  [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-type"];
  [theRequest addValue:SOAPActionStr forHTTPHeaderField:@"SOAPAction"];
  [theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
  [theRequest setHTTPMethod:@"POST"];
  [theRequest setHTTPBody:[webServiceStr dataUsingEncoding:NSUTF8StringEncoding]];
  NSURLConnection *theConn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  if (theConn) {
     
    NSLog(@"8888 Hahahaha");
  }else {
    NSLog(@"5555, cry for me");
  }
}
 //The agent that received the data
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
   
   
}
 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
   
  NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  NSLog(@"%@", str);//Get the desired XML string and parse it   
  //The system comes with  NSXMLParser *par = [[NSXMLParser alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
  [par setDelegate:self];//Set the parsing method proxy for NSXMLParser object  [par parse];//Calling the agent to parse the NSXMLParser object to see if the parsing is successful}
//Parse XML
#pragma mark xmlparser
//step 1: Prepare for analysis- (void)parserDidStartDocument:(NSXMLParser *)parser{
   
   
}
//step 2: Prepare to parse node- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
  NSLog(@"%@", NSStringFromSelector(_cmd) );
}
//step 3: Get the content between the head and tail nodes- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
  NSLog(@"%@", string);
}
 
//step 4: parsing the current node- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
  NSLog(@"%@",NSStringFromSelector(_cmd) );
}
 
//step 5; end of analysis- (void)parserDidEndDocument:(NSXMLParser *)parser{
   
}
//Get cdata block data- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{
   
}

The above content is the way to call WebService (SOAP interface) using IOS, hoping to inspire you.