以下のリンクに従って、ログイン ページの 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];
}
ログインに失敗して戻ります。
前もって感謝します。