Skip to content

Swift Developer Blog

Learn Swift and App Development for iOS

  • Swift Blog
    • UITableView
    • UIImageView
    • UIScrollView
    • UIWebView
    • SubView
    • UIImagePickerController
    • UIPageViewController
    • UITabBarController
    • UIAlertController
    • UISegmentedControl
    • Xcode
    • Firebase
  • Video Tutorials
  • Code Examples
    • Code examples
    • Cheat Sheets
  • Resources
    • Swift Developer
  • Full stack
    • Developer Resources
    • Android & Kotlin
    • Java
    • RESTful Web Services
    • Amazon Web Services
    • Firebase with Kotlin
    • Firebase with Swift
    • Hibernate
    • Git
    • Design Patterns
  • About
  • Contact me
  • Home
  • Cocoa Touch and Swift tutorials
  • Generate User Password in Swift
March 29, 2017 by Sergey Kargopolov 0 comments on "Generate User Password in Swift"

Generate User Password in Swift

When user signs up for an account with our mobile app, we invite them to come up with a secure password. It is user friendly, if our app can suggest them a secure password by generating one and placing next to a password text field, so that they can pick it and use.

The code below will help you generate an alphanumeric string of random characters which you can use to auto generate a secure password for your users. Also, the code below allows you to generate a password of desired length. Although the longer the password the more difficult it is to brute force it, a very long password would be difficult for user to remember. In my opinion 8-10 characters password is a good balance.

Generate User Password Code Example in Swift

func generatePassword(passwordLength: Int) -> String {
    
    let passwordMaterial : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    let passwordMaterialLength = UInt32(passwordMaterial.length)
    
    var returnValue = ""
    
    for _ in 0 ..< passwordLength {
        let randomPosition = arc4random_uniform(passwordMaterialLength)
        var character = passwordMaterial.character(at: Int(randomPosition))
        returnValue += NSString(characters: &character, length: 1) as String
    }
    
    return returnValue
}


let myPassword = generatePassword(passwordLength: 10)
print("Secure password: \(myPassword)")

I hope this short code example is helpful to you!

Building Mobile Apps for iOS with Swift – Video Courses


Posted in: Cocoa Touch and Swift tutorials, User login and Registration with Swift
Tagged: Password

Post navigation

Previous Previous post: UIPageViewController. Create and Skip App Tutorial Pages.
Next Next post: Meet AppsDeveloperBlog.com

Primary Sidebar

Find videos

Unit Testing Swift?

Unit Testing Swift Mobile App

Recent Posts

  • Inject JavaScript into WKWebView
  • WKWebView. Load HTML File from App Bundle.
  • UIImagePickerController in Swift with Firebase. Cheat Sheet.
  • Firebase Realtime Database Cheat Sheet
  • Push Notifications with Firebase Cloud Messaging – Cheat Sheet

Search by Tag

AWS Facebook Facebook SDK Facebook Sign in Firebase HTTP HTTP Get HTTP Post Icon Java Keychain Load Images Login MMDrawerController MySQL Navigation Drawer NSURLSession Parse Password PHP Push Notifications Search Sign in SubView Swift UIActivityIndicator UIAlertController UIImage UIImagePickerController UIImageView UIPageViewController UIScrollView UISearchBar UISegmentedControl UITabBarController UITableView UITableViewCell UITableViewController UITableViewDatasource UIWebView Upload Xcode Xcode Tips Xcode Tips and Tricks XIB

Featured posts

  • Left side menu (Navigation Drawer) example with Swift
  • Creating custom user interface files with XIB in Xcode 6 and Swift
  • Add Subview and Remove subview example in Swift
  • Customize UINavigationBar appearance in Swift
  • MBProgressHUD example in Swift
  • iOS Keychain example in Swift
  • Image Upload with Progress Bar example in Swift
  • Circular Image or Image with Rounded Corners. Example in Swift

Copyright © 2023 Swift Developer Blog. Powered by WordPress and Themelia.

Would you like more video tutorials weekly?
  • Swift programming with Parse. Practical Examples.
  • Swift programming with PHP and MySQL. Practical examples.
  • iOS Mobile Apps Development with Swift

Enter your email and stay on top of things,

Subscribe!