2009-10-17 3 views

ответ

5

Этот онлайн-сервис предоставит вам .NET WSDL и создаст объекты Objective-C для использования в вашем приложении для iPhone.

http://sudzc.com/

+0

Лучше всего когда-либо – Bryan

+0

Только примечание: sudzc имеет проблемы с сервиса WSDL, WPF, но это, кажется обрабатывать ASMX те , – Mortoc

+0

@ChulBulPandey Не могли бы вы объяснить? Сайт все еще работает, и источник по-прежнему доступен на GitHub ... –

1

(пожалуйста, поправьте случай кодирования в вашем Xcode) Вот код, который вы должны осуществить в любой из вашей функции говорят -(void) viewDidLoad{}

  1. Сначала вам нужно позвонить веб а затем немногие из его делегатов
  2. Во-вторых, когда вы получаете данные, вам приходится разбирать их, потому что данные поступают в формате XML, поэтому используйте XML-синтаксис (NSXMLparser).
  3. После того, как у вас есть данные, вы можете отобразить их или что хотите.

Ниже приведен пример кода, чтобы получить HELLO WORLD строку возвращения из веб-службы:

- (void)viewDidload 
{ 
    NSMutableString *soapMessage = [NSMutableString stringWithFormat:@"<?xml version=\"1.0\"encoding=\"utf-8\"?>\n" 
     "<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/\">\n" 
     "<soap:Body>\n" 
     "<HelloWorld xmlns=\"http://Helloworldservice.org/\" />\n" 
     "</soap:Body>\n" 
     "</soap:Envelope>"]; 

    //You can get soap Message from web service simply when you run it it shows the request message and response message in XML format copy request message then paste it in XCODE. (Notice please check for escape sequence that is \") 

    // The URL of the your web service 
    NSURL *url = [NSURL URLWithString:@"http://Helloworldservice/service.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://HelloWorld.org/HelloWorld" 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) //Means if the connection is active 
    { 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 
} 

//NOW You have to implement some of delegates required for connection 

//Recieve data defined when your connection do recieve some data from .NET web service. 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSString* strin=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 
    //Now as we get data in XML format time to parse the data into some meaningful form use NSXMLPARSER. 
    NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data]; 

    //Delegate should be defined as now make it self it will probably give a warning message so execute it no problem but if u want this warning to be removed just ass delegate in .h file like @interface Web_Service_testViewController : UIViewController<NSXMLParserDelegate> {} 

    [parser setDelegate:self]; 
    //here actual parsing is done. 
    [parser parse]; 
    //Now time to release parse object from memory. 
    [parser release]; 
} 

//If connection fails just in case OK. 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{} 

    //Called when connection has finished. Mostly we place the data in this delegate we require to be called after web service i hope it is understandable to u :-) 

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

//Now we have to implement some of the delegates of NSXMLPARSER Quite simple 

//For a file reading. 
- (void)parserDidStartDocument:(NSXMLParser *)parser{ 
} 

//When parsing is started on the element obtained from web service for the moment leave it blank. 
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
} 

//When parsing is finished. 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
} 

//This is the main delegate that can return a string format result. 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)astring 
{ 
    //Say you have a NSString object named as "mymessage" 
    mymessage=string; 
    //this "astring" is that string which is returned from delegate 
} 

//Your web service is finished it is totally implemented code no errors :-). Quite easy isn't it? 
Смежные вопросы