当我们在iOS应用程序中使用Objective C输入错误的安全代码时如何显示警报

问题描述 投票:0回答:6

我有一个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获得响应的参考,因此在获取响应下方,我需要保持警惕。

在输入错误的安全代码时,在这种情况下如何显示警报?

iphone objective-c cocos2d-iphone
6个回答
0
投票

尝试使用此代码...

-(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
            }
        }
    }
}

0
投票

您想要做的是在获得响应的地方完成的。

在响应中,您将有一个变量通知成功或失败。

检查该值。

  • 如果该值表示成功,则继续。]
  • 失败,然后显示“身份验证失败为警报。
  • 快乐编码


0
投票

实际上,您实际上在命名特定方法时,我不明白为什么需要标记。也许代码可以像下面这样简单?


0
投票

您必须检查来自Web服务数据的响应,以获取有效和无效的代码。


0
投票

您必须检查您的回复。它是否在提供所需的答案。


0
投票
-(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;
}
© www.soinside.com 2019 - 2024. All rights reserved.