0

我正在按照以下链接解析登录页面的 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];
}

并返回登录失败。

提前致谢。

4

2 回答 2

0

您首先需要解决如何使用解析器从xml解析一些东西的解析,然后考虑登录,因为如果您制作了解析器,那么使用登录表单很容易。下面是XML解析的以下教程,它很详细,可以帮助您获得帮助。

http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/

希望这可以帮助

于 2012-04-23T11:49:31.717 回答
0

只需检查一下

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"User"]) {
     NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
    [item setValue:[attributeDict valueForKey:@"username"] forKey:@"username"];
      [item setValue:[attributeDict valueForKey:@"password"] forKey:@"password"];
     [users addObject:item];
}

}

请删除其他两名代表并检查

于 2012-04-23T11:53:40.450 回答