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

Iphone 如何在iOS应用程序中使用Objective-C输入错误的安全代码时显示警报,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我有一个webservicesurl,它附带了securecode。当我输入正确的安全代码(例如说abcd)时,应用程序将加载。当我输入错误的安全代码时,我需要将警报显示为“错误的安全代码”。我的代码在这里: - (void) alertStatus:(NSString *)msg :(NSString *)Title:(int)tag { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Title

我有一个webservicesurl,它附带了securecode。当我输入正确的安全代码(例如说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获取响应的引用。因此,在获取响应的下面,我需要保持警惕


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

您要做的是在收到响应的地方完成

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

检查该值

  • 如果该值指示成功,则继续
  • 失败,然后显示“身份验证失败为警报”

快乐编码

我不明白,当您实际命名一个特定的方法时,为什么您需要一个标记

- (void) alertStatus:(NSString *)msg :(NSString *)Title {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Title
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
[alertView show];
return;
}

您必须检查来自web服务数据的响应是否存在有效和无效代码

您将使用以下方法:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    value = [[NSString alloc] initWithBytes: [webdata mutableBytes] length:[webdata length] encoding:NSUTF8StringEncoding];
    NSLog(@"==>%@", value);

    if ([value isEqualToString:@"Valid"]) {
       NSLog(@"Valid");
    }
    else {
       NSLog(@"Invlid");

       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Title 
                                               message:msg
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
       [alertView show];
    }

    [webData release];
    webData = nil;

    [connection release];
    connection = nil;
}
根据“值”,您可以检查您的有效代码和无效代码

希望对你有帮助


谢谢。

你必须检查一下你的回答,看它是否给出了你想要的答案

--如果没有给出正确的答案,您可以在此处显示您的alertView,否则您可以继续进一步的逻辑


谢谢你,希望这对你有所帮助。

尝试使用此代码

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

你需要自定义你的验证逻辑…这不在你的代码中…基于你可以管理的响应。你可以使用你在responseData中获得的数据,然后在此基础上,你可以显示警报在失败的情况下你将得到什么样的响应?@BhanuPrakash,在成功的情况下,我将从web服务获得响应,这是一个“url”w这里和失败案例一样,如果密码错误,我没有得到响应。因此需要显示警报。对于失败案例,我得到警报,这很好。但是当我给出正确的安全代码时,我得到的是“500内部服务器错误。您要找的资源有问题”错误。请尝试将服务url放在浏览器中输入有效的安全代码,检查服务是否返回正确响应。超过500个-内部服务器错误完全属于服务器端问题,不属于ios。是的,当我在浏览器中检查url时,它返回正确响应。在代码中,我没有在日志中获得输出。没有修改代码的Hai正在工作ng.实际上响应是一个需要使用web view访问的url。我在else语句中编写了代码,就好像响应数据是我们需要的一样。通过在日志中查看,我也得到了url。但没有在web view中加载url的内容。如上所述显示错误…现在将响应url放在浏览器中,并检查该url是否正确是否正在调整数据。