Skip to content

Instantly share code, notes, and snippets.

@dacur
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dacur/ee34ea6403c2f8eb41b9 to your computer and use it in GitHub Desktop.

Select an option

Save dacur/ee34ea6403c2f8eb41b9 to your computer and use it in GitHub Desktop.
A generic signup / login form for an iOS app written in Swift. NOTE: This uses Parse for user management. Put your Parse credentials in your AppDelegate.swift file.
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var signupActive = true
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
func displayAlert(title: String, error:String){
var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
@IBOutlet weak var username: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var alreadyRegistered: UILabel!
@IBOutlet weak var signUpLabel: UILabel!
@IBOutlet weak var signUpButton: UIButton!
@IBAction func toggleSignUp(sender: AnyObject) {
if signupActive == true {
signupActive = false
signUpLabel.text = "Use the form below to log in"
signUpButton.setTitle("Log In", forState: UIControlState.Normal)
alreadyRegistered.text = "Not registered?"
signUpToggleButton.setTitle("Sign Up", forState: UIControlState.Normal)
} else {
signupActive = true
signUpLabel.text = "Use the form below to sign up"
signUpButton.setTitle("Sign Up", forState: UIControlState.Normal)
alreadyRegistered.text = "Already registered?"
signUpToggleButton.setTitle("Log In", forState: UIControlState.Normal)
}
}
@IBOutlet weak var signUpToggleButton: UIButton!
@IBAction func signUp(sender: AnyObject) {
var error = ""
if username.text == "" || password.text == "" {
error = "Please enter a username and password."
}
if countElements(username.text) < 6 {
error = "Username must be at least six characters"
}
if countElements(password.text) < 6 {
error = "Password must be at least six characters."
}
if error != "" {
displayAlert("Error in form.", error: error)
} else {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
if signupActive == true {
var user = PFUser()
user.username = username.text
user.password = password.text
//user.email = "email@example.com"
// other fields can be set just like with PFObject
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, signupError: NSError!) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
println("signed up")
// Hooray! Let them use the app now.
} else {
if let errorString = signupError.userInfo?["error"] as? NSString{
error = errorString
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Sign Up", error: error)
}
}
} else {
PFUser.logInWithUsernameInBackground(username.text, password: password.text) {
(user: PFUser!, signupError: NSError!) -> Void in
self.activityIndicator.stopAnimating()
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if signupError == nil {
// Do stuff after successful login.
println("logged in")
} else {
if let errorString = signupError.userInfo?["error"] as? NSString{
error = errorString
} else {
error = "Please try again later."
}
self.displayAlert("Could Not Log In", error: error)
}
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
} // end viewDidLoad
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment