简体   繁体   English

如何在iPhone SDK中隐含验证码变异

[英]How to impliment captcha varification in iphone sdk

I have a task to work with captcha in my application which gives the same process of verification in my mobile application like website captcha process. 我有一个在我的应用程序中使用验证码的任务,该任务在移动应用程序中提供了与网站验证码过程相同的验证过程。

My question is to develop or integrate captcha in my application. 我的问题是在我的应用程序中开发或集成验证码。 and my hole code is developed in native iphone language. 我的漏洞代码是用iphone本地语言开发的。 I have search a lot for integration of captcha in mobile application. 我已经搜索了很多验证码在移动应用程序中的集成。 But i haven't find any reference. 但是我没有找到任何参考。

So please help me for this to develop captcha in my application without using webview.And provide some code or sample for it. 因此,请帮助我在不使用webview的情况下在我的应用程序中开发验证码,并提供一些代码或示例。

use this http://iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx find a nice background image for you app, and just randomly generate some number and/or text. 使用此http://iphonesdksnippets.com/post/2009/05/05/Add-text-to-image-%28UIImage%29.aspx为您的应用找到漂亮的背景图片,并随机生成一些数字和/或文本。 not exactly a captcha, but it can work out. 不完全是一个验证码,但是可以解决。 Hope it helps! 希望能帮助到你!

Though there is no Need to add Captcha in some Application, as Applications are not as like Web, So, as per my thinking there is no Need to attach the Captcha in some Application to prevent the Bots, Still if you need to embed it... Yes, Here is the Possible way, Please check the following codes: 尽管不需要在某些应用程序中添加验证码,因为应用程序不像Web,因此,按照我的想法,不需要在某些应用程序中附加验证码以防止Bot,但是如果您需要将其嵌入。 ..是的,这是可能的方法,请检查以下代码:

Take these outlets and variables: 使用以下出口和变量:

NSArray *arrCapElements;
IBOutlet UILabel *Captcha_label;
IBOutlet UITextField *Captcha_field;
IBOutlet UILabel *Status_label;

and IBActions as: IBActions为:

- (IBAction)Reload_Action:(id)sender;
- (IBAction)Submit_Action:(id)sender;

In storyboard choose the font name as Chalkduster 30.0 for the Captcha_label . 在故事板选择字体名称Chalkduster 30.0Captcha_label

Now assign arrCapElements in viewDidLoad() as 现在在viewDidLoad()中将arrCapElements分配为

arrCapElements = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];

Code for load the Captcha: 加载验证码的代码:

-(void)load_captcha{
    @try {
        CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
        Captcha_label.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
        //Captcha_label.textColor=[UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
        NSUInteger elmt1,elmt2,elmt3,elmt4,elmt5,elmt6;
        elmt1 = arc4random() % [arrCapElements count];
        elmt2= arc4random() % [arrCapElements count];
        elmt3 = arc4random() % [arrCapElements count];
        elmt4 = arc4random() % [arrCapElements count];
        elmt5 = arc4random() % [arrCapElements count];
        elmt6 = arc4random() % [arrCapElements count];

        NSString *Captcha_string = [NSString stringWithFormat:@"%@%@%@%@%@%@",arrCapElements[elmt1-1],arrCapElements[elmt2-1],arrCapElements[elmt3-1],arrCapElements[elmt4-1],arrCapElements[elmt5-1],arrCapElements[elmt6-1]];
        //NSLog(@" Captcha String : %@",Captcha_string);
        Captcha_label.text = Captcha_string;
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
}

Reload Action: 重新加载动作:

- (IBAction)Reload_Action:(id)sender {

    [self load_captcha];
}

Check the captcha Correct or not: 检查验证码是否正确:

- (IBAction)Submit_Action:(id)sender {

    NSLog(@"%@ = %@",Captcha_label.text,Captcha_field.text);
    if([Captcha_label.text isEqualToString: Captcha_field.text]){
        [self.view endEditing:YES];
        Status_label.text =@"Success";
        Status_label.textColor = [UIColor greenColor];
    }else{
        Status_label.text =@"Faild";
        Status_label.textColor = [UIColor redColor];
    }
}

It will be shown like this: 它将显示如下:

演示画面

Help taken from: Captcha Generator for iOS 取自帮助: iOS的Captcha Generator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM