We are building iOS application using Swift as language and myPHPadmin as databa
ID: 3681693 • Letter: W
Question
We are building iOS application using Swift as language and myPHPadmin as database.
Currently, we finished initiaive framework (Register, log in, forget password) on Xcode and successfully link with the databse.
The problem we had faced was, we can not figure out how to validate the duplicate data.
For example, if the user is trying to register the email that already exist in our database, the application should alert the user that the email already exist.
Here is our Xcode framework and please let us know how to fix this problem.
//
// RegisterAccountVC.swift
// TimeDice
//
// Created by Team.Midas.K. on 1/22/16.
// Copyright © 2016 Midas.K. All rights reserved.
//
import UIKit
import Parse
class RegisterAccountVC: UIViewController {
@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBOutlet var reenterTextField: UITextField!
@IBOutlet var bgScrollView: UIScrollView!
@IBOutlet var contentView: UIView!
@IBOutlet var registerButton: UIButton!
var keyshow:Bool = false
var database = Database()
override func viewDidLoad() {
super.viewDidLoad()
// Exit from keyboard
let tapGesture = UITapGestureRecognizer(target: self, action: "tap:")
view.addGestureRecognizer(tapGesture)
//keyboard adjustment
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor.blueColor()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("tap:"))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
emailTextField.inputAccessoryView = toolBar
passwordTextField.inputAccessoryView = toolBar
reenterTextField.inputAccessoryView = toolBar
registerButton.addTarget(self, action: "tap:", forControlEvents: UIControlEvents.TouchUpInside)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
}
@IBAction func registerButtonClicked(sender: AnyObject) {
// Check for empty fields
if emailTextField.text == "" || passwordTextField.text == "" || reenterTextField.text == ""
{
//Display alert msg
let alertController = UIAlertController(title: "Fill Empty Field", message: "You need to fill all field to register", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: {})
} else if passwordTextField.text != reenterTextField.text {
let alertController = UIAlertController(title: "Password is not correct", message: "Please check password and re-enter password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: {})
} else if !isRightPassword(passwordTextField.text!) {
let alertController = UIAlertController(title: "Wrong Password Type", message: "Please user 8 or more words, no special character", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: {})
} else if !isValidEmail(emailTextField.text!) {
let alertController = UIAlertController(title: "Wrong Email Type", message: "Please check email", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: {})
} else {
//sending information to SQL using PHP
database.registerId = emailTextField.text!
database.registerPw = passwordTextField.text!
database.registerDBConnectSave{(isResponse) -> Void in
if isResponse {
print("User registered")
let alertController = UIAlertController(title: "Registration Success!", message: "Please verify your email before login", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default,handler: {(action: UIAlertAction!) in
let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(false, completion: {
presentingViewController!.dismissViewControllerAnimated(true, completion: {})})
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
print("User registration Failed")
let alertController = UIAlertController(title: "Username exist", message: "Please use other username", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
}
}
}
@IBAction func toLoginClicked(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func backButtonClicked(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
// Exit from keyboard
func tap(gesture: UITapGestureRecognizer) {
emailTextField.resignFirstResponder()
passwordTextField.resignFirstResponder()
reenterTextField.resignFirstResponder()
}
// Keyboard and frame adjustment
func keyboardWillShow(notification:NSNotification) {
if keyshow == false {
adjustingHeight(true, notification: notification)
}
}
func keyboardWillHide(notification:NSNotification) {
if keyshow == true {
adjustingHeight(false, notification: notification)
}
}
func adjustingHeight(show:Bool, notification:NSNotification) {
let userInfo = notification.userInfo!
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let changeInHeight = (CGRectGetHeight(keyboardFrame)) * (show ? 1 : -1)
if show == true {
bgScrollView.contentInset.bottom += changeInHeight
bgScrollView.scrollIndicatorInsets.bottom += changeInHeight
keyshow = true
} else {
bgScrollView.contentInset.bottom += changeInHeight
bgScrollView.scrollIndicatorInsets.bottom += changeInHeight
keyshow = false
}
}
// Check Email type
func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(testStr)
}
// Check Password type
func isRightPassword(testStr:String) -> Bool {
let passwordRegEx = "[\w]{8,}"
let passTest = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passTest.evaluateWithObject(testStr)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Explanation / Answer
Do one thing . while registering keep that email ID in one variable and in login page keep that in another variable compare these two in login code .
Or else , try Java script we can display immediately after the text box whether he is entering valid email ID r not . it is easy using JavaScript . just try . if u want the logic in js , just reply me I will let u know .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.