How could we integrate the Captcha in iOS application?

前端 未结 3 842
旧时难觅i
旧时难觅i 2020-12-29 08:45

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

3条回答
  •  情歌与酒
    2020-12-29 09:37

    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

提交回复
热议问题