1

我在由 NSObject 继承的单独类中编写我的 post 方法,我在其中发送两个参数,即用户名和密码以及 viewcontroller 的引用作为委托。我应该如何在这个类中构造初始化方法。通常我在这个类中使用本机解析器进行解析。

到目前为止,我一直在这样做:

-(void)initWith:(NSString *)username password:(NSString*)password delegate:(id<name>)delegate

{

NSString *soapMessage = [[NSString alloc] initWithFormat:

                         @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                         "<soapenv:Envelope \n"
                         "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
                         "xmlns:tem=\"http://tempuri.org/\"> \n"
                         "<soapenv:Header/>\n"
                         "<soapenv:Body>\n"
                         "<tem:Login>\n"

                         "<tem:username>%@</tem:username>\n"

                         "<tem:password>%@</tem:password>\n"

                         "</tem:Login>\n"

                         "</soapenv:Body>\n"

                         "</soapenv:Envelope>\n",hisUDID,myUDID];



NSURL *url = [NSURL URLWithString:kBaseURL];               
// NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:url];

NSString *msgLength = [[NSString alloc] initWithFormat:@"%d", [soapMessage length]];          
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
[theRequest addValue: @"http://tempuri.org/XXXXX/Login" forHTTPHeaderField:@"Soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];     
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[msgLength release];
[soapMessage release];

[theRequest release];
theRequest=nil;

if(theConnection)
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"The Connection is NULL");
}

}

一般来说,我已经看到初始化程序具有返回类型 id,它初始化了一些需要的 sata 结构......请提供建议,因为我想学习一种构造 init 方法的好方法!

4

0 回答 0