我有一个webservices网址,后面附有安全代码。当我输入正确的安全代码(例如abcd)时,应用程序将加载。输入错误的安全代码时,我需要将警报显示为“错误的安全代码”。我的代码在这里:
- (void) alertStatus:(NSString *)msg :(NSString *)Title:(int)tag
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
if(tag)
alertView.tag = tag;
[alertView show];
}
-(IBAction)loginClicked:(id)sender {
@try {
if([[txtsecurecode text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter Access code" :@"Login Failed!":0];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"txtsecurecode=%@",[txtsecurecode text]];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidAccess?accesscode=%@&type=1",txtsecurecode.text]];
NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
responseData是从Web服务url获得响应的参考,因此在获取响应下方,我需要保持警惕。
在输入错误的安全代码时,在这种情况下如何显示警报?
尝试使用此代码...
-(IBAction)loginClicked:(id)sender {
@try {
if([[txtsecurecode text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter Access code" :@"Login Failed!":0];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"txtsecurecode=%@",[txtsecurecode text]];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://my example.com/Accountservice/Security/ValidAccess?accesscode=%@&type=1",txtsecurecode.text]];
/*UPDATED*/
NSString *responseData = [[NSString alloc]initWithContentsOfURL:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding error:nil];
if([responseData isEqualToString:@""])
{
[self alertStatus:@"Please enter valid Access code" :@"Login Failed!":0];
}
else
{
//Your code for handling the response data
}
}
}
}
您想要做的是在获得响应的地方完成的。
在响应中,您将有一个变量通知成功或失败。
检查该值。
快乐编码
实际上,您实际上在命名特定方法时,我不明白为什么需要标记。也许代码可以像下面这样简单?
您必须检查来自Web服务数据的响应,以获取有效和无效的代码。
您必须检查您的回复。它是否在提供所需的答案。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding])
if ([responseString isEqualToString:@"Success"]) {
NSLog(@"Success");
}
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alertView show];
}
[responseData release];
responseData = nil;
[connection release];
connection = nil;
}