wsdl2objc
地址:http://code.google.com/p/wsdl2objc/
服務器端,參考:【Web Service】Apache Tuscany發布Web Service
准備工作:
svn checkout http://wsdl2objc.googlecode.com/svn/trunk/
<生成代碼
運行WSDLParser項目
WSDL欄輸入wsdl的地址
Output Location欄輸入輸出代碼的目錄
點擊Parse WSDL按鈕生成代碼:
<添加到項目
<支持libxml2
TARGETS -> Build Settings -> Linking -> Other Linker Flags,設置“-lxml2”
TARGETS -> Building Settings -> Apple LLVM compiler 4.1 - Language -> Other C Flags,設置“-I/usr/include/libxml2”
<添加framework
TARGETS -> Build Phases -> Link Binary With Libraries,添加CFNetwork.framework
代碼示例
#import <UIKit/UIKit.h> #import "IHelloWorldService.h" @interface ViewController : UIViewController <IHelloWorldServiceBindingResponseDelegate> { UITextField *mNameTextField; UITextView *mMessageTextView; } @property (retain, nonatomic) IBOutlet UITextField *nameTextField; @property (retain, nonatomic) IBOutlet UITextView *messageTextView; - (IBAction)sendButtonPressed:(id)sender; @end
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize nameTextField = mNameTextField, messageTextView = mMessageTextView; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)sendButtonPressed:(id)sender { IHelloWorldServiceBinding *binding = [IHelloWorldService IHelloWorldServiceBinding]; IHelloWorldService_say *say = [[IHelloWorldService_say new] autorelease]; say.arg0 = [mNameTextField text]; [binding sayAsyncUsingSay:say delegate:self]; } - (void)operation:(IHelloWorldServiceBindingOperation *)operation completedWithResponse:(IHelloWorldServiceBindingResponse *)response { NSArray *responseHeaders = response.headers; NSArray *responseBodyParts = response.bodyParts; for(id header in responseHeaders) { // here do what you want with the headers, if there's anything of value in them } for(id bodyPart in responseBodyParts) { if ([bodyPart isKindOfClass:[SOAPFault class]]) { // continue; } if([bodyPart isKindOfClass:[IHelloWorldService_sayResponse class]]) { IHelloWorldService_sayResponse *body = (IHelloWorldService_sayResponse*)bodyPart; NSString *text = body.return_; mMessageTextView.text = [NSString stringWithFormat:@"%@\n%@", mMessageTextView.text, text]; continue; } } } @end
Build,報錯:"libxml/tree.h" file not found
解決方法:
PROJECT -> Build Settings -> Search Paths -> Header Search Paths,設置“${SDK_DIR}/usr/include/libxml2”
TARGETS -> Build Settings -> Search Paths -> Header Search Paths,設置“${SDK_DIR}/usr/include/libxml2”
參考:http://mmz06.blog.163.com/blog/static/121416962012913202818/
Build,Run
請求,參數為“Anthony”
相應,內容為“Hello null”
wsdl2objc Bug!!!
解決方法:修改文件IHelloWorldService.m中,類“IHelloWorldService_say”的“- (void)addElementsToNode:(xmlNodePtr)node”方法。
設置元素命名空間前綴為“nil”
xmlAddChild(node, [self.arg0xmlNodeForDoc:node->docelementName:@"arg0"elementNSPrefix:nil]);
Build,Run
成功!!!