I googled for integration of Captcha in iOS app but do not found any relevant way to do so. Even i sign up with reCAPTCHA and searched whether the plugins for Captcha are av
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:
Take these outlets and variables:
NSArray *arrCapElements;
IBOutlet UILabel *Captcha_label;
IBOutlet UITextField *Captcha_field;
IBOutlet UILabel *Status_label;
and IBActions
as:
- (IBAction)Reload_Action:(id)sender;
- (IBAction)Submit_Action:(id)sender;
In storyboard choose the font name as Chalkduster 30.0 for the Captcha_label
.
Now assign arrCapElements
in viewDidLoad()
as
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
Though we do not have any of the API for Captcha in iOS , not even the reCaptcha has provided any plugin for 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,
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.
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.
For my I use the following library:
https://github.com/fjcaetano/ReCaptcha
it will use a hidden webview to handle that