Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // DataViewController.swift
- // Sprint2Register
- //
- // Created by CSSE Department on 10/2/14.
- // Copyright (c) 2014 CSSE Department. All rights reserved.
- //
- import UIKit
- class LoginDataViewController: UIViewController, NSURLSessionDelegate {
- @IBOutlet weak var dataLabel: UILabel!
- var dataObject: AnyObject?
- @IBOutlet weak var nameLabel: UILabel!
- @IBOutlet weak var emailLabel: UILabel!
- @IBOutlet weak var passLabel: UILabel!
- @IBOutlet weak var cpassLabel: UILabel!
- @IBOutlet weak var nameValidation: UILabel!
- @IBOutlet weak var emailValidation: UILabel!
- @IBOutlet weak var passValidation: UILabel!
- @IBOutlet weak var cpassValidation: UILabel!
- @IBOutlet weak var emailInUseValidation: UILabel!
- @IBOutlet weak var passConfValidation: UILabel!
- @IBOutlet weak var nameTextField: UITextField!
- @IBOutlet weak var emailTextField: UITextField!
- @IBOutlet weak var passTextField: UITextField!
- @IBOutlet weak var cpassTextField: UITextField!
- var user : User?
- func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
- println("Failed with error")
- }
- func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
- println("challenged")
- let credential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
- completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
- }
- func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
- println("Finished events for background")
- }
- @IBAction func registerClicked(AnyObject){
- var error = false
- if !nameValidation.hidden || !emailValidation.hidden || !passValidation.hidden || !cpassValidation.hidden || !passConfValidation.hidden || !emailInUseValidation.hidden || nameTextField.text.isEmpty || emailTextField.text.isEmpty || passTextField.text.isEmpty || cpassTextField.text.isEmpty {
- error = true
- }
- if !error {
- var data : NSDictionary = ["email" : emailTextField.text, "username": nameTextField.text, "password" : passTextField.text/*, "nickname" : nameTextField.text */]
- var err: NSError?
- var jsonData = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted, error: &err)
- let username = "share@rose-hulman.edu"
- let password = "share"
- let loginString = NSString(format: "%@:%@", username, password)
- let loginData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
- let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromMask(0))
- // create the request
- var urlPath = "http://datass.io:5006/register" //"http://137.112.104.213:8000/users/"
- var url: NSURL = NSURL(string: urlPath)
- var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
- //request.HTTPMethod = "GET" //I don't know if this and line below apply. I can't check since db is currently down
- //request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
- let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
- var session = NSURLSession.sharedSession() //NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
- request.HTTPBody = jsonData
- request.addValue("application/json", forHTTPHeaderField: "Content-Type")
- request.addValue("application/json", forHTTPHeaderField: "Accept")
- request.HTTPMethod = "POST"
- //let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
- //let challenge = NSURLAuthenticationMethodServerTrust
- //let cred = NSURLCredential.credentialWithUser("", password: "", persistence: NSURLCredentialPersistence.Permanent)
- //let space = NSURLProtectionSpace(host: "datass.io", port: 5000, `protocol`: "https", realm: nil, authenticationMethod: NSURLAuthenticationMethodServerTrust)
- //sessionConfiguration.URLCredentialStorage?.setCredential(cred, forProtectionSpace: space)
- //let session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
- // let basicAuthCred = ("Basic share@rose-hulman.edu:share")
- // request.setValue(basicAuthCred, forHTTPHeaderField:"Authorization")
- var confirm : NSDictionary?
- let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
- println("Registration Task completed")
- if(error != nil) {
- // If there is an error in the web request, print it to the console
- println(error.localizedDescription)
- }
- var err: NSError?
- println(response)
- var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
- confirm = jsonResult
- println(jsonResult)
- dispatch_async(dispatch_get_main_queue(), {
- self.navigationController?.popViewControllerAnimated(true)
- return
- })
- })
- task.resume();
- var emailRegister = UIAlertView(title: "Email Confirmation", message: "An account confirmation email has been sent to your email", delegate: nil, cancelButtonTitle: "close")
- emailRegister.show();
- //SPECIAL CALL TO CONFIRM Email
- //NEEDS TO BE REMOVED ONCE EMAIL CONFIRMATION IS WORKING
- data = ["confirmation_token" : confirm["confirm"]]
- urlPath = "http://datass.io:5006/confirm_email" //"http://137.112.104.213:8000/users/"
- url = NSURL(string: urlPath)
- request = NSMutableURLRequest(URL: url)
- session = NSURLSession.sharedSession() //NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
- request.HTTPBody = jsonData
- request.addValue("application/json", forHTTPHeaderField: "Content-Type")
- request.addValue("application/json", forHTTPHeaderField: "Accept")
- request.HTTPMethod = "POST"
- let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
- println("Confirmation Task completed")
- if(error != nil) {
- // If there is an error in the web request, print it to the console
- println(error.localizedDescription)
- }
- var err: NSError?
- println(response)
- var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
- confirm = jsonResult
- println(jsonResult)
- dispatch_async(dispatch_get_main_queue(), {
- self.navigationController?.popViewControllerAnimated(true)
- return
- })
- })
- task.resume();
- }
- }
- @IBAction func comparePassword(AnyObject){
- if(passTextField.text != cpassTextField.text){
- passConfValidation.hidden = false;
- }
- else {
- passConfValidation.hidden = true;
- }
- if countElements(cpassTextField.text)>512 || countElements(cpassTextField.text)<6 {
- cpassValidation.hidden = false;
- }
- else {
- cpassValidation.hidden = true;
- }
- }
- @IBAction func emailValid(AnyObject){
- }
- @IBAction func usernameLength(AnyObject){
- if countElements(nameTextField.text)>512 || countElements(nameTextField.text)<1 {
- nameValidation.hidden = false;
- }
- else {
- nameValidation.hidden = true;
- }
- }
- @IBAction func passwordLength(AnyObject){
- if countElements(passTextField.text)>512 || countElements(passTextField.text)<6 {
- passValidation.hidden = false;
- }
- else {
- passValidation.hidden = true;
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- @IBAction func backgroundTap(sender: AnyObject){
- self.view.endEditing(true)
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- if let obj: AnyObject = dataObject {
- self.dataLabel!.text = obj.description
- } else {
- self.dataLabel!.text = ""
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement