2013-03-19 49 views
0

我有一个附加了securecode的webservices url。当我输入正确的安全码例如说abcd时,应用程序将加载。当我输入错误的安全代码时,我需要将警告显示为'错误的安全代码'。我的代码是在这里不用..如何在ios应用程序中使用目标c输入错误的安全代码时显示警报

- (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就是我正在从网络服务响应url.so下方获得响应的参考,我需要保持警觉。

你能帮我在这种情况下保持警觉,当输入错误的安全代码时显示警报。提前致谢。

+0

u需要定制UR验证逻辑...这是不是在我们的代码.. 。 – Ganapathy 2013-03-19 05:57:53

+0

根据您可以管理的响应。 – Balu 2013-03-19 05:58:59

+0

您可以使用您在responseData中获得的数据,然后在此基础上,您可以显示警报 – Kasaname 2013-03-19 06:00:33

回答

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

对于失败的情况,我得到警觉这很好。但是当我给出正确的安全代码时,我得到'500-内部服务器错误。您正在查找的资源存在问题'错误。 – 2013-03-19 07:07:46

+0

只是尝试使用有效的安全代码在服务器中放置服务URL,并检查服务是否返回正确的响应。超过500-内部服务器错误完全属于服务器端问题不属于ios。 – 2013-03-19 07:11:49

+0

是的,当我在浏览器中检查url时,它返回正确的响应。在代码中,我没有得到日志中的输出。 – 2013-03-19 07:14:08

0

你想要做的是在你得到答复的地方完成。

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

检查该值。

  • 如果该值指示是否表明然后
  • 故障显示“验证失败为成功警报.Proceed。

快乐编码

0

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

​​
0

您必须检查来自网络服务数据的响应,才能生效&无效的代码。

你会得到这种方法::

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

基于 '价值',你可以检查你的有效 & 无效代码。

希望它能帮助你。

谢谢。

0

你必须检查你的答案,不管它是否给你想要的答案。

- 如果它没有给出正确的答案,你可以在这里显示你的alertView,否则你可以继续进一步的逻辑。

谢谢,希望这有助于

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; 
} 
相关问题