1

我正在使用 ASIHttprequest 进行登录身份验证这是 deme 代码

我正在尝试的代码是:

[self setRequest:[ASIHTTPRequest requestWithURL:url1]];
[_request setUseKeychainPersistence:[useKeychain isOn]];
[_request setDelegate:self];
[_request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[_request setDidFinishSelector:@selector(topSecretFetchComplete:)];
[_request setDidFailSelector:@selector(topSecretFetchFailed:)];
[_request startAsynchronous];
[request setValidatesSecureCertificate:NO];
[_request setValidatesSecureCertificate:NO];

请求失败和请求完成方法

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
   [responseField setText:[[request error] localizedDescription]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
   [responseField setText:[request responseString]];
   [responseField setFont:[UIFont boldSystemFontOfSize:12]];
   NSLog(@"Response :%@",[request responseString]);

}

- (void)authenticationNeededForRequest:(ASIHTTPRequest *)theRequest{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please Login" message:[request authenticationRealm] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];

[alertView addTextFieldWithValue:@"" label:@"Username"];
[alertView addTextFieldWithValue:@"" label:@"Password"];
[alertView show];

}

我是 ios 新手,所以请......
在这个演示中,当我开始计时时会弹出一个警报,但是当我使用我的网络服务时,它会给出这样的响应

响应:ASIHTTPRequest:0xa846400

但不要弹出用于输入用户名和密码的警报视图

如果有任何其他更好的方式来验证登录请建议我

我想将登录名设置为rootview,成功登录后可以进入应用程序

谢谢...

4

2 回答 2

1

有时会在响应中包含空格、特殊字符和换行符时发生这种情况,因此首先修剪该字符串,然后像下面这样打印响应。

NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Response :%@",strResponse);

更新:

- (NSString *)flattenHTML:(NSString *)html {

    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];

    while ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString:@"<" intoString:NULL] ; 

        [theScanner scanUpToString:@">" intoString:&text] ;

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    //
    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    return html;
}

像下面这样使用这种方法..

 NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 NSString *strFinal = [self flattenHTML:strResponse];
 NSLog(@"Response :%@",strFinal);
于 2013-08-01T09:19:50.603 回答
0

对于登录身份验证,使用 ASIFormDataRequest 遵循以下步骤

1)创建一个可以匹配服务器的用户名和密码的网络服务,如果用户名和密码正确,则给出响应。给出响应“成功”否则“失败”。

例如:https ://www.yoursite.com/login1.php?username=%@&password=%@

2) 像这样使用 UsernameTextfield.text 和 PasswordTextfield.text 创建一个 url

NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.yoursite.com/login1.php?username=%@&password=%@",Ustr,Pstr]];

其中 Ustr 和 Pstr 是来自两个文本字段的 NSString。

3) 创建 ASIFormDataRequest

- (IBAction)fetchTopSecretInformation:(id)sender{

    Ustr = User.text;
    Pstr  = Pass.text;

    NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.yoursite.com/login1.php?username=%@&password=%@",Ustr,Pstr]];
    ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1];
    [request1 setUseKeychainPersistence:YES];
    request1.delegate = self;
    request1.shouldPresentCredentialsBeforeChallenge = YES;

    [request1 setRequestMethod:@"POST"];
    [request1 setPostValue:User.text forKey:@"User Name"];
    [request1 setPostValue:Pass.text forKey:@"Password"];
    [request1 setTimeOutSeconds:30];

    [request1 setDidFailSelector:@selector(topSecretFetchFailed:)];
    [request1 setDidFinishSelector:@selector(topSecretFetchComplete:)];
    [request1 startAsynchronous];

}

将此方法与 xib 中的登录按钮连接。

- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
    NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    strFinal = [theRequest responseString];
    NSLog(@"Sattus Code : %@",[theRequest responseString]);

    NSString *failStr = @"failed";
    NSString *successStr = @"successfully";

    if ([strFinal isEqualToString:successStr]) {
    ViewController *view = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
      [self.navigationController pushViewController:view animated:YES];

   }else
        if ([strFinal isEqualToString:failStr]) {
            UIAlertView *AlertFail = [[UIAlertView alloc] initWithTitle:@"LogIn Failed !" message:@"Wrong ID or Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [AlertFail show];
        }
}

因此,如果成功转到下一页,否则警告警告.....

于 2013-08-07T06:31:01.957 回答