在处理 Web 服务调用时,我发现很难从一个页面导航到另一个页面。这是我的问题,
我能够成功调用 Web 服务并获取 xml,还能够解析 xml 并获取所需的标签。我的问题是我在按钮操作中完成所有这些过程。Web 服务和解析完成后,我想导航页面。但我不能这样做,因为我的页面在服务调用和解析完成之前正在导航。
下面是我正在做的代码
- (IBAction)CIS_Login:(id)sender
{
WebServiceAuth *service = [[WebServiceAuth alloc]init];
NSString *username,*password;
username = CIS_Username.text;
password = CIS_Password.text;
[service getUsername:username getPassword:password]; (Calling Web service)
NSString *getSessionToken = [USERDEFAULTS objectForKey:@"sessionToken"];
if (getSessionToken)
{
[self receivedSessionTokenFromCloud];
}
else
{
[self receivedErrorMessageFromCloud];
}
}
网络服务页面
-(void)getUsername:(NSString *)usernameAuth getPassword:(NSString *)passwordAuth
{
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<session xmlns=\"http://schemas.XYZ.com/ABC/2011\">"
"<account>\n"
"<username>%@</username>\n"
"<password>%@</password>\n"
"</account>\n"
"</session>",usernameAuth,passwordAuth];
NSLog(@"%@",soapMessage);
NSURL *url = [NSURL URLWithString:@"https://XYZ.com/sessions"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"Token" forHTTPHeaderField:@"Code"];
[theRequest addValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"application/xml" forHTTPHeaderField:@"Accept"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",[theRequest allHTTPHeaderFields]);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
Data = [NSMutableData data];
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [Data length]);
NSString *theXML = [[NSString alloc] initWithBytes: [Data mutableBytes] length:[CiscoData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
UserOAuthXML *xmlOAuth = [[UserOAuthXML alloc]init]; (Xml Parsing is done here)
[xmlOAuth getDataFromUserOAuth:Data];
}
Xml 解析器页面
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"code"])
{
recordResult = FALSE;
NSLog(@"%@",soapResults);
[USERDEFAULTS setValue:soapResults forKey:@"code"];
}
else if ([elementName isEqualToString:@"message"])
{
recordResult = FALSE;
NSLog(@"%@",soapError);
[USERDEFAULTS removeObjectForKey:@"value"];
}
}
我希望你能理解我的询问。谢谢