当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS本地动态生成验证码的方法

iOS本地动态生成验证码的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

烽火三国英雄出处,pepsitmall,丽人坊古装

前几天app注册被人攻击了,从网上找了这个先保存下。。。。

用于ios本地动态生成验证码,效果如下:

demo.gif

  • 导入coregraphics.framework

用于绘制图形

  • 封装uiview,便捷使用,代码如下:
authcodeview.h
#import <uikit/uikit.h>
@interface authcodeview : uiview
@property (strong, nonatomic) nsarray *dataarray;//字符素材数组
@property (strong, nonatomic) nsmutablestring *authcodestr;//验证码字符串
@end
authcodeview.m
#import "authcodeview.h"
#define krandomcolor [uicolor colorwithred:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
#define klinecount 6
#define klinewidth 1.0
#define kcharcount 6
#define kfontsize [uifont systemfontofsize:arc4random() % 5 + 15]
@implementation authcodeview
/*
// only override drawrect: if you perform custom drawing.
// an empty implementation adversely affects performance during animation.
- (void)drawrect:(cgrect)rect {
  // drawing code
}
*/
- (instancetype)initwithframe:(cgrect)frame
{
  self = [super initwithframe:frame];
  if (self)
  {
    self.layer.cornerradius = 5.0f;
    self.layer.maskstobounds = yes;
    self.backgroundcolor = krandomcolor;
    [self getauthcode];//获得随机验证码
  }
  return self;
}
#pragma mark 获得随机验证码
- (void)getauthcode
{
  //字符串素材
  _dataarray = [[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];
  _authcodestr = [[nsmutablestring alloc] initwithcapacity:kcharcount];
  //随机从数组中选取需要个数的字符串,拼接为验证码字符串
  for (int i = 0; i < kcharcount; i++)
  {
    nsinteger index = arc4random() % (_dataarray.count-1);
    nsstring *tempstr = [_dataarray objectatindex:index];
    _authcodestr = (nsmutablestring *)[_authcodestr stringbyappendingstring:tempstr];
  }
}
#pragma mark 点击界面切换验证码
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
  [self getauthcode];
  //setneedsdisplay调用drawrect方法来实现view的绘制
  [self setneedsdisplay];
}
- (void)drawrect:(cgrect)rect
{
  [super drawrect:rect];
  //设置随机背景颜色
  self.backgroundcolor = krandomcolor;
  //根据要显示的验证码字符串,根据长度,计算每个字符串显示的位置
  nsstring *text = [nsstring stringwithformat:@"%@",_authcodestr];
  cgsize csize = [@"a" sizewithattributes:@{nsfontattributename:[uifont systemfontofsize:20]}];
  int width = rect.size.width/text.length - csize.width;
  int height = rect.size.height - csize.height;
  cgpoint point;
  //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等
  float px,py;
  for ( int i = 0; i<text.length; i++)
  {
    px = arc4random() % width + rect.size.width/text.length * i;
    py = arc4random() % height;
    point = cgpointmake(px, py);
    unichar c = [text characteratindex:i];
    nsstring *textc = [nsstring stringwithformat:@"%c", c];
    [textc drawatpoint:point withattributes:@{nsfontattributename:kfontsize}];
  }
   //调用drawrect:之前,系统会向栈中压入一个cgcontextref,调用uigraphicsgetcurrentcontext()会取栈顶的cgcontextref
  cgcontextref context = uigraphicsgetcurrentcontext();
  //设置线条宽度
  cgcontextsetlinewidth(context, klinewidth);
  //绘制干扰线
  for (int i = 0; i < klinecount; i++)
  {
    uicolor *color = krandomcolor;
    cgcontextsetstrokecolorwithcolor(context, color.cgcolor);//设置线条填充色
    //设置线的起点
    px = arc4random() % (int)rect.size.width;
    py = arc4random() % (int)rect.size.height;
    cgcontextmovetopoint(context, px, py);
    //设置线终点
    px = arc4random() % (int)rect.size.width;
    py = arc4random() % (int)rect.size.height;
    cgcontextaddlinetopoint(context, px, py);
    //画线
    cgcontextstrokepath(context);
  }
}
@end
  • 界面添加验证码
@interface authcodeviewcontroller ()<uitextfielddelegate, uialertviewdelegate>
{
  authcodeview *authcodeview;
  uitextfield *_input;
}
@end
@implementation authcodeviewcontroller
- (void)viewdidload {
  [super viewdidload];
  // do any additional setup after loading the view.
  self.view.backgroundcolor = [uicolor whitecolor];
  //显示验证码界面
  authcodeview = [[authcodeview alloc] initwithframe:cgrectmake(30, 100, self.view.frame.size.width-60, 40)];
  [self.view addsubview:authcodeview];
  //提示文字
  uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(50, 160, self.view.frame.size.width-100, 40)];
  label.text = @"点击图片换验证码";
  label.font = [uifont systemfontofsize:12];
  label.textcolor = [uicolor graycolor];
  [self.view addsubview:label];
  //添加输入框
  _input = [[uitextfield alloc] initwithframe:cgrectmake(30, 220, self.view.frame.size.width-60, 40)];
  _input.layer.bordercolor = [uicolor lightgraycolor].cgcolor;
  _input.layer.borderwidth = 2.0;
  _input.layer.cornerradius = 5.0;
  _input.font = [uifont systemfontofsize:21];
  _input.placeholder = @"请输入验证码!";
  _input.clearbuttonmode = uitextfieldviewmodewhileediting;
  _input.backgroundcolor = [uicolor clearcolor];
  _input.textalignment = nstextalignmentcenter;
  _input.returnkeytype = uireturnkeydone;
  _input.delegate = self;
  [self.view addsubview:_input];
}
#pragma mark 输入框代理,点击return 按钮
- (bool)textfieldshouldreturn:(uitextfield *)textfield
{
  //判断输入的是否为验证图片中显示的验证码
  if ([_input.text isequaltostring:authcodeview.authcodestr])
  {
    //正确弹出警告款提示正确
    uialertview *alview = [[uialertview alloc] initwithtitle:@"恭喜您 ^o^" message:@"验证成功" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];
    [alview show];
  }
  else
  {
    //验证不匹配,验证码和输入框抖动
    cakeyframeanimation *anim = [cakeyframeanimation animationwithkeypath:@"transform.translation.x"];
    anim.repeatcount = 1;
    anim.values = @[@-20,@20,@-20];
//    [authcodeview.layer addanimation:anim forkey:nil];
    [_input.layer addanimation:anim forkey:nil];
  }
  return yes;
}
#pragma mark 警告框中方法
-(void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex
{
  //清空输入框内容,收回键盘
  if (buttonindex==0)
  {
    _input.text = @"";
    [_input resignfirstresponder];
  }
}

以上所述是小编给大家介绍的ios本地动态生成验证码的方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网