我正在使用 ASIHttprequest 进行登录身份验证 Here is the deme code
我正在尝试的代码是:
[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 的新手所以请......
在此演示中,当我走钟时会弹出一个警报,但是当我使用我的网络服务时,它会给出这样的响应
Response :ASIHTTPRequest: 0xa846400
但不要弹出输入用户名和密码的警告 View
如果有任何其他更好的方法来验证登录,请建议我
我想以rootview登录,成功登录后可以进入应用
谢谢...
最佳答案
有时会出现空格、特殊字符和换行符在响应中的情况,因此首先修剪该字符串,然后像下面这样打印响应。
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);
关于iphone - ASIHttprequest 用于登录认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17988312/