我们如何将验证码集成到 iOS 应用程序中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20972283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could we integrate the Captcha in iOS application?
提问by iShwar
I googled for integration of Captchain iOS app but do not found any relevant way to do so. Even i sign up with reCAPTCHAand searched whether the plugins for Captcha are available for iOS or not? I didn't found any plugin for iOS there. Somewhere while RND i come to know that "its not needed for the mobile apps" , But the client wants the Captcha in his application so ,I want to know :-
我用Google搜索的集成验证码的iOS应用,但也没有发现任何相关的方式这样做。即使我注册了reCAPTCHA并搜索了 Captcha 的插件是否适用于 iOS?我没有在那里找到任何适用于 iOS 的插件。在 RND 的某个地方,我开始知道“移动应用程序不需要它”,但是客户希望在他的应用程序中使用验证码,所以,我想知道:-
Whether we can integrate the Captcha in iOS App/Mobile Apps :-
If Yes? : Then what would be the relevant way to integrate it.
If No? : Then what is the relevant reason .
我们是否可以在 iOS 应用程序/移动应用程序中集成验证码:-
如果是?: 那么什么是相关的整合方式。
如果不?: 那么相关的原因是什么。
采纳答案by iShwar
Though we do not have any of the API for Captcha in iOS , not even the reCaptchahas provided any pluginfor iOS . And even lot of peoples suggested me that there is no need to implement the captcha for mobile applications. I agreed, but as the client was not be able to listen any excuses what i have done is,
尽管我们在 iOS 中没有任何用于 Captcha 的 API,但即使是reCaptcha也没有为 iOS提供任何插件。甚至很多人建议我不需要为移动应用程序实现验证码。我同意了,但由于客户无法听取任何借口,我所做的是,
simply i created the Random number using
-(NSInteger)randomIntBetween:(NSInteger)min and:(NSInteger)max { return (NSInteger)(min + arc4random_uniform(max + 1 - min)); }
placed that number on label and asked user to enter the same number in in provided textField, If the match is found the captcha's principal is done [The user on the other end is human being is the only principal to integrate captcha], Other wise i regenerate the random number and place it there again on the label asking user to enter same number in provided textField.
Final Result will be like this.
只是我创建了随机数使用
-(NSInteger)randomIntBetween:(NSInteger)min and:(NSInteger)max { return (NSInteger)(min + arc4random_uniform(max + 1 - min)); }
将该数字放在标签上并要求用户在提供的文本字段中输入相同的数字,如果找到匹配,则验证码的主体完成[另一端的用户是人类是唯一集成验证码的主体],否则我重新生成随机数并将其再次放在标签上,要求用户在提供的文本字段中输入相同的数字。
最终结果会是这样。
Now the idea might be more clear to you, Simply assign the output of the function "randomIntBetween"to the label on which the number is displayed, and when the user will press the login button authenticate whether the text in the textField(Named: Enter number) is matches with the label on its left, if it matches allow the user to login if not then clear then text of text field and also the text on label then assign the new random number to the label on left by calling the method "randomIntBetween:"and ask user to enter the text in the enter number text field.
现在这个想法对你来说可能更清楚,只需将函数“randomIntBetween”的输出分配给显示数字的标签,当用户按下登录按钮时,验证 textField(Named: Enter number) 与其左侧的标签匹配,如果匹配则允许用户登录,如果不匹配则清除文本字段的文本以及标签上的文本,然后通过调用方法将新的随机数分配给左侧的标签“ randomIntBetween:"并要求用户在输入数字文本字段中输入文本。
回答by Developer So far
For my I use the following library:
对于我,我使用以下库:
https://github.com/fjcaetano/ReCaptcha
https://github.com/fjcaetano/ReCaptcha
it will use a hidden webview to handle that
它将使用隐藏的 webview 来处理
回答by Manab Kumar Mal
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,所以,根据我的想法,不需要在某些应用程序中附加验证码来防止机器人,如果您需要嵌入它。 .. 是的,这是可能的方法,请检查以下代码:
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.0for the Captcha_label
.
在故事板选择字体名称Chalkduster 30.0对Captcha_label
。
Now assign arrCapElements
in viewDidLoad()
as
现在,将arrCapElements
在viewDidLoad()
如
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 验证码生成器