全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

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

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

  • 导入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本地动态生成验证码的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# ios本地生成验证码  # iOS开发实现随机图片验证码封装  # iOS 生成图片验证码绘制实例代码  # iOS 生成图片验证码(实用功能)  # 利用iOS绘制图片生成随机验证码示例代码  # IOS实现验证码倒计时功能(一)  # iOS获取短信验证码倒计时的两种实现方法  # IOS实现验证码倒计时功能(二)  # iOS生成图片数字字母验证效果  # 验证码  # 输入框  # 小编  # 在此  # 被人  # 给大家  # 请输入  # 弹出  # 找了  # 前几天  # 来实现  # 所述  # 给我留言  # 框中  # 感谢大家  # 会向  # 清空  # 组中  # 不匹配  # 疑问请 


相关文章: 建站与域名管理如何高效结合?  建站10G流量真的够用吗?如何应对访问高峰?  唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?  公司网站的制作公司,企业网站制作基本流程有哪些?  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  如何选择香港主机高效搭建外贸独立站?  免费网站制作appp,免费制作app哪个平台好?  如何用5美元大硬盘VPS安全高效搭建个人网站?  建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南  专业公司网站制作公司,用什么语言做企业网站比较好?  如何快速搭建响应式可视化网站?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  青浦网站制作公司有哪些,苹果官网发货地是哪里?  ,柠檬视频怎样兑换vip?  如何快速生成ASP一键建站模板并优化安全性?  如何在云服务器上快速搭建个人网站?  网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?  邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?  建站主机如何选?高性价比方案全解析  宝华建站服务条款解析:五站合一功能与SEO优化设置指南  天津个人网站制作公司,天津网约车驾驶员从业资格证官网?  小程序网站制作需要准备什么资料,如何制作小程序?  如何在阿里云通过域名搭建网站?  如何选择高效便捷的WAP商城建站系统?  网站设计制作企业有哪些,抖音官网主页怎么设置?  如何在云虚拟主机上快速搭建个人网站?  相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?  定制建站哪家更专业可靠?推荐榜单揭晓  如何用PHP快速搭建CMS系统?  如何在宝塔面板中修改默认建站目录?  如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?  *服务器网站为何频现安全漏洞?  详解jQuery中基本的动画方法  黑客如何利用漏洞与弱口令入侵网站服务器?  建站主机选购指南:核心配置与性价比推荐解析  建站之星CMS五站合一模板配置与SEO优化指南  如何选择最佳自助建站系统?快速指南解析优劣  如何在阿里云完成域名注册与建站?  专业网站制作企业网站,如何制作一个企业网站,建设网站的基本步骤有哪些?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  教学网站制作软件,学习*后期制作的网站有哪些?  在线制作视频网站免费,都有哪些好的动漫网站?  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?  武清网站制作公司,天津武清个人营业执照注销查询系统网站?  香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  建站主机CVM配置优化、SEO策略与性能提升指南  如何通过西部建站助手安装IIS服务器?  ,怎么用自己头像做动态表情包? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。