我正在按照以下链接解析登录页面的 xml, http: //yksoftware.blogspot.in/2010/04/iphone-programming-tutorial-xml-login.html
每当我输入用户名和密码字段时,即使我在文本字段中提供正确的用户名和密码,它也会显示登录失败。在下面的代码中,当我在 loginPressed 方法中保留断点并检查时,它没有进入 for 循环,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
users = [[NSMutableArray alloc]init];
NSURL *xmlURL = [NSURL URLWithString:@"http://www.mailrail.net/sample.aspx?username=naresh&password=reddy"];
xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"User"]) {
item = [[NSMutableDictionary alloc] init];
currentUser =[[NSMutableString alloc] init];
currentPassword =[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"User"]) {
[item setObject:currentUser forKey:@"username"];
[item setObject:currentPassword forKey:@"password"];
[users addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:@"username"]) {
[currentUser appendString:string];
}
if ([currentElement isEqualToString:@"password"]) {
[currentPassword appendString:string];
}
}
-(IBAction)loginPressed:(id)sender{
for (NSMutableDictionary *val in users) {
NSMutableString *usrname = [val objectForKey:@"username"];
NSLog(@"%@",usrname);
NSMutableString *psswrd = [val objectForKey:@"password"];
usrname=[usrname stringByReplacingOccurrencesOfString:@"\n" withString:@""];
usrname=[usrname stringByReplacingOccurrencesOfString:@"\t" withString:@""];
psswrd=[psswrd stringByReplacingOccurrencesOfString:@"\n" withString:@""];
psswrd=[psswrd stringByReplacingOccurrencesOfString:@"\t" withString:@""];
if([usrname isEqualToString:[txtUsername text]]&&[psswrd isEqualToString:[txtPassword text]]){
[lblLoginStatus setText:@"Login Successful!!"];
return;
}
}
[lblLoginStatus setText:@"login failed"];
return;
}
-(IBAction)returnClicked:(UITextField *)sender{
[sender resignFirstResponder];
}
-(IBAction)clickBackground:(id)sender{
[txtPassword resignFirstResponder];
[txtUsername resignFirstResponder];
}
并返回登录失败。
提前致谢。