├── .gitignore ├── LICENSE ├── README.md ├── SFVerificationCodeView-Demo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── SFVerificationCodeView-Demo ├── AppDelegate.h ├── AppDelegate.m ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── SFVerificationCodeView │ ├── SFVerificationCodeView.h │ └── SFVerificationCodeView.m ├── ViewController.h ├── ViewController.m └── main.m ├── SFVerificationCodeView ├── SFVerificationCodeView.h └── SFVerificationCodeView.m └── s.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SFVerificationCodeView 2 | ============== 3 | 4 | SFVerificationCodeView,实现iOS本地生成随机验证码, 从服务器读取并生成验证码 5 | 6 | 验证码其实主要用来防止机器人恶意注册与登陆,基本用于web网页端 7 | 8 | ![image](https://raw.githubusercontent.com/shaojiankui/SFVerificationCodeView/master/s.png) 9 | 10 | ## XIB拖拽方式 11 | 12 | ``` 13 | //设置验证码生成模式 服务器端生成验证码 14 | self.codeView.mode = SFVerificationCodeModeServer; 15 | [self.codeView willChangeVerificationCode:^(SFVerificationCodeMode mode) { 16 | NSLog(@"请求服务器生成code"); 17 | [self getData:nil completion:^(NSDictionary *dict) { 18 | self.codeView.code = [dict objectForKey:@"code"]; 19 | }]; 20 | }]; 21 | [self.codeView didChangeVerificationCode:^(NSString *code) { 22 | NSLog(@"self.codeView code:%@",code); 23 | }]; 24 | //开始生成code 25 | [self.codeView generateVerificationCode]; 26 | 27 | //模拟异步从后台接口请求验证码 28 | - (void)getData:(id)data completion:(void (^)(NSDictionary *dict))completion{ 29 | //网络请求 30 | NSString *code = [NSString stringWithFormat:@"server%zd",arc4random() % 100]; 31 | NSDictionary *dict = @{@"code":code}; 32 | completion(dict); 33 | } 34 | ``` 35 | 36 | ## 代码方式 37 | 38 | ``` 39 | //设置验证码生成模式 本地生成验证码 40 | view.mode = SFVerificationCodeModeLocal; 41 | [view willChangeVerificationCode:^(SFVerificationCodeMode mode) { 42 | NSLog(@"本地随机生成code"); 43 | 44 | }]; 45 | [view didChangeVerificationCode:^(NSString *code) { 46 | NSLog(@"view code:%@",code); 47 | 48 | }]; 49 | //开始生成code 50 | [view generateVerificationCode]; 51 | ``` 52 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A28D7D2A1A57FB9600A69EC7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A28D7D291A57FB9600A69EC7 /* main.m */; }; 11 | A28D7D2D1A57FB9600A69EC7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A28D7D2C1A57FB9600A69EC7 /* AppDelegate.m */; }; 12 | A28D7D301A57FB9600A69EC7 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A28D7D2F1A57FB9600A69EC7 /* ViewController.m */; }; 13 | A28D7D331A57FB9600A69EC7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A28D7D311A57FB9600A69EC7 /* Main.storyboard */; }; 14 | A28D7D351A57FB9600A69EC7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A28D7D341A57FB9600A69EC7 /* Images.xcassets */; }; 15 | A28D7D381A57FB9600A69EC7 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = A28D7D361A57FB9600A69EC7 /* LaunchScreen.xib */; }; 16 | B89FBD351EA0A82100ED8360 /* SFVerificationCodeView.m in Sources */ = {isa = PBXBuildFile; fileRef = B89FBD341EA0A82100ED8360 /* SFVerificationCodeView.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | A28D7D241A57FB9600A69EC7 /* SfVerificationCodeView-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "SfVerificationCodeView-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | A28D7D281A57FB9600A69EC7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 22 | A28D7D291A57FB9600A69EC7 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | A28D7D2B1A57FB9600A69EC7 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | A28D7D2C1A57FB9600A69EC7 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | A28D7D2E1A57FB9600A69EC7 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 26 | A28D7D2F1A57FB9600A69EC7 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 27 | A28D7D321A57FB9600A69EC7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | A28D7D341A57FB9600A69EC7 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 29 | A28D7D371A57FB9600A69EC7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 30 | B89FBD331EA0A82100ED8360 /* SFVerificationCodeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SFVerificationCodeView.h; sourceTree = ""; }; 31 | B89FBD341EA0A82100ED8360 /* SFVerificationCodeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SFVerificationCodeView.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | A28D7D211A57FB9600A69EC7 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | A28D7D1B1A57FB9600A69EC7 = { 46 | isa = PBXGroup; 47 | children = ( 48 | A28D7D261A57FB9600A69EC7 /* SFVerificationCodeView-Demo */, 49 | A28D7D251A57FB9600A69EC7 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | A28D7D251A57FB9600A69EC7 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | A28D7D241A57FB9600A69EC7 /* SfVerificationCodeView-Demo.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | A28D7D261A57FB9600A69EC7 /* SFVerificationCodeView-Demo */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | B89FBD321EA0A82100ED8360 /* SFVerificationCodeView */, 65 | A28D7D2B1A57FB9600A69EC7 /* AppDelegate.h */, 66 | A28D7D2C1A57FB9600A69EC7 /* AppDelegate.m */, 67 | A28D7D2E1A57FB9600A69EC7 /* ViewController.h */, 68 | A28D7D2F1A57FB9600A69EC7 /* ViewController.m */, 69 | A28D7D311A57FB9600A69EC7 /* Main.storyboard */, 70 | A28D7D341A57FB9600A69EC7 /* Images.xcassets */, 71 | A28D7D361A57FB9600A69EC7 /* LaunchScreen.xib */, 72 | A28D7D271A57FB9600A69EC7 /* Supporting Files */, 73 | ); 74 | path = "SFVerificationCodeView-Demo"; 75 | sourceTree = ""; 76 | }; 77 | A28D7D271A57FB9600A69EC7 /* Supporting Files */ = { 78 | isa = PBXGroup; 79 | children = ( 80 | A28D7D281A57FB9600A69EC7 /* Info.plist */, 81 | A28D7D291A57FB9600A69EC7 /* main.m */, 82 | ); 83 | name = "Supporting Files"; 84 | sourceTree = ""; 85 | }; 86 | B89FBD321EA0A82100ED8360 /* SFVerificationCodeView */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | B89FBD331EA0A82100ED8360 /* SFVerificationCodeView.h */, 90 | B89FBD341EA0A82100ED8360 /* SFVerificationCodeView.m */, 91 | ); 92 | path = SFVerificationCodeView; 93 | sourceTree = ""; 94 | }; 95 | /* End PBXGroup section */ 96 | 97 | /* Begin PBXNativeTarget section */ 98 | A28D7D231A57FB9600A69EC7 /* SfVerificationCodeView-Demo */ = { 99 | isa = PBXNativeTarget; 100 | buildConfigurationList = A28D7D471A57FB9600A69EC7 /* Build configuration list for PBXNativeTarget "SfVerificationCodeView-Demo" */; 101 | buildPhases = ( 102 | A28D7D201A57FB9600A69EC7 /* Sources */, 103 | A28D7D211A57FB9600A69EC7 /* Frameworks */, 104 | A28D7D221A57FB9600A69EC7 /* Resources */, 105 | ); 106 | buildRules = ( 107 | ); 108 | dependencies = ( 109 | ); 110 | name = "SfVerificationCodeView-Demo"; 111 | productName = "RandomCode-Demo"; 112 | productReference = A28D7D241A57FB9600A69EC7 /* SfVerificationCodeView-Demo.app */; 113 | productType = "com.apple.product-type.application"; 114 | }; 115 | /* End PBXNativeTarget section */ 116 | 117 | /* Begin PBXProject section */ 118 | A28D7D1C1A57FB9600A69EC7 /* Project object */ = { 119 | isa = PBXProject; 120 | attributes = { 121 | LastUpgradeCheck = 0610; 122 | ORGANIZATIONNAME = www.skyfox.org; 123 | TargetAttributes = { 124 | A28D7D231A57FB9600A69EC7 = { 125 | CreatedOnToolsVersion = 6.1; 126 | }; 127 | }; 128 | }; 129 | buildConfigurationList = A28D7D1F1A57FB9600A69EC7 /* Build configuration list for PBXProject "SFVerificationCodeView-Demo" */; 130 | compatibilityVersion = "Xcode 3.2"; 131 | developmentRegion = English; 132 | hasScannedForEncodings = 0; 133 | knownRegions = ( 134 | en, 135 | Base, 136 | ); 137 | mainGroup = A28D7D1B1A57FB9600A69EC7; 138 | productRefGroup = A28D7D251A57FB9600A69EC7 /* Products */; 139 | projectDirPath = ""; 140 | projectRoot = ""; 141 | targets = ( 142 | A28D7D231A57FB9600A69EC7 /* SfVerificationCodeView-Demo */, 143 | ); 144 | }; 145 | /* End PBXProject section */ 146 | 147 | /* Begin PBXResourcesBuildPhase section */ 148 | A28D7D221A57FB9600A69EC7 /* Resources */ = { 149 | isa = PBXResourcesBuildPhase; 150 | buildActionMask = 2147483647; 151 | files = ( 152 | A28D7D331A57FB9600A69EC7 /* Main.storyboard in Resources */, 153 | A28D7D381A57FB9600A69EC7 /* LaunchScreen.xib in Resources */, 154 | A28D7D351A57FB9600A69EC7 /* Images.xcassets in Resources */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | /* End PBXResourcesBuildPhase section */ 159 | 160 | /* Begin PBXSourcesBuildPhase section */ 161 | A28D7D201A57FB9600A69EC7 /* Sources */ = { 162 | isa = PBXSourcesBuildPhase; 163 | buildActionMask = 2147483647; 164 | files = ( 165 | A28D7D301A57FB9600A69EC7 /* ViewController.m in Sources */, 166 | B89FBD351EA0A82100ED8360 /* SFVerificationCodeView.m in Sources */, 167 | A28D7D2D1A57FB9600A69EC7 /* AppDelegate.m in Sources */, 168 | A28D7D2A1A57FB9600A69EC7 /* main.m in Sources */, 169 | ); 170 | runOnlyForDeploymentPostprocessing = 0; 171 | }; 172 | /* End PBXSourcesBuildPhase section */ 173 | 174 | /* Begin PBXVariantGroup section */ 175 | A28D7D311A57FB9600A69EC7 /* Main.storyboard */ = { 176 | isa = PBXVariantGroup; 177 | children = ( 178 | A28D7D321A57FB9600A69EC7 /* Base */, 179 | ); 180 | name = Main.storyboard; 181 | sourceTree = ""; 182 | }; 183 | A28D7D361A57FB9600A69EC7 /* LaunchScreen.xib */ = { 184 | isa = PBXVariantGroup; 185 | children = ( 186 | A28D7D371A57FB9600A69EC7 /* Base */, 187 | ); 188 | name = LaunchScreen.xib; 189 | sourceTree = ""; 190 | }; 191 | /* End PBXVariantGroup section */ 192 | 193 | /* Begin XCBuildConfiguration section */ 194 | A28D7D451A57FB9600A69EC7 /* Debug */ = { 195 | isa = XCBuildConfiguration; 196 | buildSettings = { 197 | ALWAYS_SEARCH_USER_PATHS = NO; 198 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 199 | CLANG_CXX_LIBRARY = "libc++"; 200 | CLANG_ENABLE_MODULES = YES; 201 | CLANG_ENABLE_OBJC_ARC = YES; 202 | CLANG_WARN_BOOL_CONVERSION = YES; 203 | CLANG_WARN_CONSTANT_CONVERSION = YES; 204 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 205 | CLANG_WARN_EMPTY_BODY = YES; 206 | CLANG_WARN_ENUM_CONVERSION = YES; 207 | CLANG_WARN_INT_CONVERSION = YES; 208 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 209 | CLANG_WARN_UNREACHABLE_CODE = YES; 210 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 211 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 212 | COPY_PHASE_STRIP = NO; 213 | ENABLE_STRICT_OBJC_MSGSEND = YES; 214 | GCC_C_LANGUAGE_STANDARD = gnu99; 215 | GCC_DYNAMIC_NO_PIC = NO; 216 | GCC_OPTIMIZATION_LEVEL = 0; 217 | GCC_PREPROCESSOR_DEFINITIONS = ( 218 | "DEBUG=1", 219 | "$(inherited)", 220 | ); 221 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 222 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 223 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 224 | GCC_WARN_UNDECLARED_SELECTOR = YES; 225 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 226 | GCC_WARN_UNUSED_FUNCTION = YES; 227 | GCC_WARN_UNUSED_VARIABLE = YES; 228 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 229 | MTL_ENABLE_DEBUG_INFO = YES; 230 | ONLY_ACTIVE_ARCH = YES; 231 | SDKROOT = iphoneos; 232 | }; 233 | name = Debug; 234 | }; 235 | A28D7D461A57FB9600A69EC7 /* Release */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | ALWAYS_SEARCH_USER_PATHS = NO; 239 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 240 | CLANG_CXX_LIBRARY = "libc++"; 241 | CLANG_ENABLE_MODULES = YES; 242 | CLANG_ENABLE_OBJC_ARC = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_CONSTANT_CONVERSION = YES; 245 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 246 | CLANG_WARN_EMPTY_BODY = YES; 247 | CLANG_WARN_ENUM_CONVERSION = YES; 248 | CLANG_WARN_INT_CONVERSION = YES; 249 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 250 | CLANG_WARN_UNREACHABLE_CODE = YES; 251 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 252 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 253 | COPY_PHASE_STRIP = YES; 254 | ENABLE_NS_ASSERTIONS = NO; 255 | ENABLE_STRICT_OBJC_MSGSEND = YES; 256 | GCC_C_LANGUAGE_STANDARD = gnu99; 257 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 258 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 259 | GCC_WARN_UNDECLARED_SELECTOR = YES; 260 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 261 | GCC_WARN_UNUSED_FUNCTION = YES; 262 | GCC_WARN_UNUSED_VARIABLE = YES; 263 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 264 | MTL_ENABLE_DEBUG_INFO = NO; 265 | SDKROOT = iphoneos; 266 | VALIDATE_PRODUCT = YES; 267 | }; 268 | name = Release; 269 | }; 270 | A28D7D481A57FB9600A69EC7 /* Debug */ = { 271 | isa = XCBuildConfiguration; 272 | buildSettings = { 273 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 274 | INFOPLIST_FILE = "SFVerificationCodeView-Demo/Info.plist"; 275 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 276 | PRODUCT_BUNDLE_IDENTIFIER = "org.skyfox.SFVerificationCodeView-Demo"; 277 | PRODUCT_NAME = "$(TARGET_NAME)"; 278 | }; 279 | name = Debug; 280 | }; 281 | A28D7D491A57FB9600A69EC7 /* Release */ = { 282 | isa = XCBuildConfiguration; 283 | buildSettings = { 284 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 285 | INFOPLIST_FILE = "SFVerificationCodeView-Demo/Info.plist"; 286 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 287 | PRODUCT_BUNDLE_IDENTIFIER = "org.skyfox.SFVerificationCodeView-Demo"; 288 | PRODUCT_NAME = "$(TARGET_NAME)"; 289 | }; 290 | name = Release; 291 | }; 292 | /* End XCBuildConfiguration section */ 293 | 294 | /* Begin XCConfigurationList section */ 295 | A28D7D1F1A57FB9600A69EC7 /* Build configuration list for PBXProject "SFVerificationCodeView-Demo" */ = { 296 | isa = XCConfigurationList; 297 | buildConfigurations = ( 298 | A28D7D451A57FB9600A69EC7 /* Debug */, 299 | A28D7D461A57FB9600A69EC7 /* Release */, 300 | ); 301 | defaultConfigurationIsVisible = 0; 302 | defaultConfigurationName = Release; 303 | }; 304 | A28D7D471A57FB9600A69EC7 /* Build configuration list for PBXNativeTarget "SfVerificationCodeView-Demo" */ = { 305 | isa = XCConfigurationList; 306 | buildConfigurations = ( 307 | A28D7D481A57FB9600A69EC7 /* Debug */, 308 | A28D7D491A57FB9600A69EC7 /* Release */, 309 | ); 310 | defaultConfigurationIsVisible = 0; 311 | defaultConfigurationName = Release; 312 | }; 313 | /* End XCConfigurationList section */ 314 | }; 315 | rootObject = A28D7D1C1A57FB9600A69EC7 /* Project object */; 316 | } 317 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 36 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/SFVerificationCodeView/SFVerificationCodeView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SFVerificationCodeView..h 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import 10 | typedef NS_ENUM(NSUInteger, SFVerificationCodeMode) { 11 | SFVerificationCodeModeLocal, //本地code随机生成 12 | SFVerificationCodeModeServer //服务器端返回code 13 | }; 14 | typedef void(^SFVerificationCodeDidChange)(NSString *code); 15 | typedef void(^SFVerificationCodeWillChange)(SFVerificationCodeMode mode); 16 | 17 | 18 | @interface SFVerificationCodeView : UIView 19 | { 20 | SFVerificationCodeDidChange _didChangeVerificationCode; 21 | SFVerificationCodeWillChange _willChangeVerificationCode; 22 | UITapGestureRecognizer *_tap; 23 | } 24 | //验证码请求改变回调 25 | -(void)willChangeVerificationCode:(SFVerificationCodeWillChange)willChangeVerificationCode; 26 | //验证码改变回调 27 | -(void)didChangeVerificationCode:(SFVerificationCodeDidChange)didChangeVerificationCode; 28 | 29 | ///随机code长度 默认5位 30 | @property (nonatomic) NSUInteger length; 31 | //手动设置code 32 | @property (nonatomic,copy) NSString *code; 33 | //code生成方式 34 | @property (nonatomic,assign) SFVerificationCodeMode mode; 35 | //手动触发生成code 36 | - (void)generateVerificationCode; 37 | @end 38 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/SFVerificationCodeView/SFVerificationCodeView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SFVerificationCodeView..m 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import "SFVerificationCodeView.h" 10 | 11 | @implementation SFVerificationCodeView 12 | 13 | 14 | - (instancetype)initWithFrame:(CGRect)frame 15 | { 16 | self = [super initWithFrame:frame]; 17 | if (self) { 18 | [self buidView]; 19 | } 20 | return self; 21 | } 22 | 23 | -(void)awakeFromNib{ 24 | [super awakeFromNib]; 25 | [self buidView]; 26 | } 27 | 28 | -(void)buidView{ 29 | self.userInteractionEnabled = YES; 30 | _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(generateCode:)]; 31 | [self addGestureRecognizer:_tap]; 32 | _length = 5; 33 | self.backgroundColor = [UIColor whiteColor]; 34 | } 35 | 36 | - (UIColor *)randomColor { 37 | NSInteger aRedValue = arc4random() % 255; 38 | NSInteger aGreenValue = arc4random() % 255; 39 | NSInteger aBlueValue = arc4random() % 255; 40 | UIColor *randColor = [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0f]; 41 | return randColor; 42 | } 43 | -(NSString*)randomCode{ 44 | //数字: 48-57 45 | //小写字母: 97-122 46 | //大写字母: 65-90 47 | char chars[] = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ"; 48 | char codes[self.length]; 49 | 50 | for(int i=0;i 10 | #import "SFVerificationCodeView.h" 11 | @interface ViewController : UIViewController 12 | @property (weak, nonatomic) IBOutlet SFVerificationCodeView *codeView; 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view, typically from a nib. 20 | SFVerificationCodeView *view = [[SFVerificationCodeView alloc]initWithFrame:CGRectMake(100, 100, 100, 30)]; 21 | view.backgroundColor = [UIColor blackColor]; 22 | [self.view addSubview:view]; 23 | 24 | //设置验证码生成模式 默认为Local 25 | view.mode = SFVerificationCodeModeLocal; 26 | [view willChangeVerificationCode:^(SFVerificationCodeMode mode) { 27 | NSLog(@"本地随机生成code"); 28 | 29 | }]; 30 | [view didChangeVerificationCode:^(NSString *code) { 31 | NSLog(@"view code:%@",code); 32 | 33 | }]; 34 | //开始生成code 35 | [view generateVerificationCode]; 36 | 37 | self.codeView.mode = SFVerificationCodeModeServer; 38 | [self.codeView willChangeVerificationCode:^(SFVerificationCodeMode mode) { 39 | NSLog(@"请求服务器生成code"); 40 | [self getData:nil completion:^(NSDictionary *dict) { 41 | self.codeView.code = [dict objectForKey:@"code"]; 42 | }]; 43 | }]; 44 | [self.codeView didChangeVerificationCode:^(NSString *code) { 45 | NSLog(@"self.codeView code:%@",code); 46 | }]; 47 | //开始生成code 48 | [self.codeView generateVerificationCode]; 49 | } 50 | 51 | //模拟异步从后台接口请求验证码 52 | - (void)getData:(id)data completion:(void (^)(NSDictionary *dict))completion{ 53 | //.... 54 | NSString *code = [NSString stringWithFormat:@"server%zd",arc4random() % 100]; 55 | NSDictionary *dict = @{@"code":code}; 56 | completion(dict); 57 | } 58 | @end 59 | -------------------------------------------------------------------------------- /SFVerificationCodeView-Demo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /SFVerificationCodeView/SFVerificationCodeView.h: -------------------------------------------------------------------------------- 1 | // 2 | // SFVerificationCodeView..h 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import 10 | typedef NS_ENUM(NSUInteger, SFVerificationCodeMode) { 11 | SFVerificationCodeModeLocal, //本地code随机生成 12 | SFVerificationCodeModeServer //服务器端返回code 13 | }; 14 | typedef void(^SFVerificationCodeDidChange)(NSString *code); 15 | typedef void(^SFVerificationCodeWillChange)(SFVerificationCodeMode mode); 16 | 17 | 18 | @interface SFVerificationCodeView : UIView 19 | { 20 | SFVerificationCodeDidChange _didChangeVerificationCode; 21 | SFVerificationCodeWillChange _willChangeVerificationCode; 22 | UITapGestureRecognizer *_tap; 23 | } 24 | -(void)didChangeVerificationCode:(SFVerificationCodeDidChange)didChangeVerificationCode; 25 | -(void)willChangeVerificationCode:(SFVerificationCodeWillChange)willChangeVerificationCode; 26 | 27 | ///随机code长度 默认5位 28 | @property (nonatomic) NSUInteger length; 29 | //手动设置code 30 | @property (nonatomic,copy) NSString *code; 31 | //code生成方式 32 | @property (nonatomic,assign) SFVerificationCodeMode mode; 33 | //手动触发生成code 34 | - (void)generateVerificationCode; 35 | @end 36 | -------------------------------------------------------------------------------- /SFVerificationCodeView/SFVerificationCodeView.m: -------------------------------------------------------------------------------- 1 | // 2 | // SFVerificationCodeView..m 3 | // RandomCode-Demo 4 | // 5 | // Created by Jakey on 15/1/3. 6 | // Copyright (c) 2015年 www.skyfox.org. All rights reserved. 7 | // 8 | 9 | #import "SFVerificationCodeView.h" 10 | 11 | @implementation SFVerificationCodeView 12 | 13 | 14 | - (instancetype)initWithFrame:(CGRect)frame 15 | { 16 | self = [super initWithFrame:frame]; 17 | if (self) { 18 | [self buidView]; 19 | } 20 | return self; 21 | } 22 | 23 | -(void)awakeFromNib{ 24 | [super awakeFromNib]; 25 | [self buidView]; 26 | } 27 | 28 | -(void)buidView{ 29 | self.userInteractionEnabled = YES; 30 | _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(generateCode:)]; 31 | [self addGestureRecognizer:_tap]; 32 | _length = 5; 33 | self.backgroundColor = [UIColor whiteColor]; 34 | } 35 | 36 | - (UIColor *)randomColor { 37 | NSInteger aRedValue = arc4random() % 255; 38 | NSInteger aGreenValue = arc4random() % 255; 39 | NSInteger aBlueValue = arc4random() % 255; 40 | UIColor *randColor = [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0f]; 41 | return randColor; 42 | } 43 | -(NSString*)randomCode{ 44 | //数字: 48-57 45 | //小写字母: 97-122 46 | //大写字母: 65-90 47 | char chars[] = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ"; 48 | char codes[self.length]; 49 | 50 | for(int i=0;i