ios - xml 后加载 xml 响应

标签 ios nsurlrequest nsmutableurlrequest

我正在尝试访问 REST api 以查看用户是否有效。我知道 connectionDidFinishLoading 运行,但我不确定如何检查响应 (xml)。请指教。

signIn 函数开始运作

- (IBAction)signIn:(id)sender;
{
    [emailError setHidden:YES];

    if([emailAddressTxt text] && [passwordTxt text]) {
        // send user/pass to server for validation
        if([self NSStringIsValidEmail:[emailAddressTxt text]]) {
            NSString *post = [NSString stringWithFormat:@"Email=%@&Password=%@", emailAddressTxt.text, passwordTxt.text];
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:@"http://www.mySite.com/validate.php"]];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setHTTPBody:postData];
            [NSURLConnection connectionWithRequest:request delegate:self];
        }
    } else {
        // give error dialogue
        [emailError setText:@"User not found"];
        [emailError setHidden:NO];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    //[signInData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    //[signInData appendData:d];
    // updated to:
    signInData = (NSMutableData *)d;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    // Fail..
    [emailError setText:@"Connection Error"];
    [emailError setHidden:NO];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseText = [[NSString alloc] initWithData:signInData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", @"check");
    NSLog(@"%@", responseText);
}

// an example response would be:
// <string xmlns="http://thedomain.com/">invalid login</string>

最佳答案

一般解析XML你会用 NSXMLParser ,但字符串响应很简单,如“<string xmlns="http://thedomain.com/">invalid login</string>”,我假设有效登录看起来像这样:“<string xmlns="http://thedomain.com/">valid login</string>

如果是这种情况,您只需查找包含字符串 @"valid login" 的响应即可。但不包含 @"invalid login"

if (([responseText rangeOfString:@"invalid login"].location == NSNotFound) && ([responseText rangeOfString:@"valid login"].location != NSNotFound)){
    // Congrats valid
}

如果(甚至更好)成功的响应是“<string xmlns="http://thedomain.com/">successful login</string>”,那么 if 语句将变得更容易理解。

if ([responseText rangeOfString:@"successful login"].location != NSNotFound){
    // Congrats valid
}

关于ios - xml 后加载 xml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8991630/

相关文章:

ios - Xamarin.iOS 相当于 Xamarin.Android 的 CookieManager.Instance

objective-c - 如何将文本数据导入sqlite?

ios - 我可以强制 UITableView 隐藏空单元格之间的分隔符吗?

ios - 关闭 NSURLConnection 的正确方法

ios - 修改 NSHTTPURLResponse 中的 header

ios - 如何更好地识别UITextField

ios - Objective-C : View PDF File Link With UIWebView

swift - 如何扩展 URLRequest

ios - 使用 NSMutableURLRequest 将海关对象发布到 Web

ios - NSMutableUrlRequest 返回空值