Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. //
  2. // DataViewController.swift
  3. // Sprint2Register
  4. //
  5. // Created by CSSE Department on 10/2/14.
  6. // Copyright (c) 2014 CSSE Department. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class LoginDataViewController: UIViewController, NSURLSessionDelegate {
  12.  
  13. @IBOutlet weak var dataLabel: UILabel!
  14. var dataObject: AnyObject?
  15. @IBOutlet weak var nameLabel: UILabel!
  16. @IBOutlet weak var emailLabel: UILabel!
  17. @IBOutlet weak var passLabel: UILabel!
  18. @IBOutlet weak var cpassLabel: UILabel!
  19. @IBOutlet weak var nameValidation: UILabel!
  20. @IBOutlet weak var emailValidation: UILabel!
  21. @IBOutlet weak var passValidation: UILabel!
  22. @IBOutlet weak var cpassValidation: UILabel!
  23. @IBOutlet weak var emailInUseValidation: UILabel!
  24. @IBOutlet weak var passConfValidation: UILabel!
  25.  
  26.  
  27. @IBOutlet weak var nameTextField: UITextField!
  28. @IBOutlet weak var emailTextField: UITextField!
  29. @IBOutlet weak var passTextField: UITextField!
  30. @IBOutlet weak var cpassTextField: UITextField!
  31.  
  32. var user : User?
  33.  
  34. func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
  35. println("Failed with error")
  36. }
  37.  
  38. func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
  39. println("challenged")
  40. let credential = NSURLCredential(trust: challenge.protectionSpace.serverTrust!)
  41. completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
  42. }
  43.  
  44. func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
  45. println("Finished events for background")
  46. }
  47.  
  48. @IBAction func registerClicked(AnyObject){
  49. var error = false
  50. 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 {
  51. error = true
  52. }
  53.  
  54. if !error {
  55. var data : NSDictionary = ["email" : emailTextField.text, "username": nameTextField.text, "password" : passTextField.text/*, "nickname" : nameTextField.text */]
  56. var err: NSError?
  57.  
  58. var jsonData = NSJSONSerialization.dataWithJSONObject(data, options: NSJSONWritingOptions.PrettyPrinted, error: &err)
  59. let username = "share@rose-hulman.edu"
  60. let password = "share"
  61. let loginString = NSString(format: "%@:%@", username, password)
  62. let loginData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
  63. let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromMask(0))
  64.  
  65. // create the request
  66. var urlPath = "http://datass.io:5006/register" //"http://137.112.104.213:8000/users/"
  67. var url: NSURL = NSURL(string: urlPath)
  68. var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
  69. //request.HTTPMethod = "GET" //I don't know if this and line below apply. I can't check since db is currently down
  70. //request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
  71.  
  72. let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
  73. var session = NSURLSession.sharedSession() //NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
  74. request.HTTPBody = jsonData
  75. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  76. request.addValue("application/json", forHTTPHeaderField: "Accept")
  77. request.HTTPMethod = "POST"
  78. //let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
  79. //let challenge = NSURLAuthenticationMethodServerTrust
  80. //let cred = NSURLCredential.credentialWithUser("", password: "", persistence: NSURLCredentialPersistence.Permanent)
  81. //let space = NSURLProtectionSpace(host: "datass.io", port: 5000, `protocol`: "https", realm: nil, authenticationMethod: NSURLAuthenticationMethodServerTrust)
  82. //sessionConfiguration.URLCredentialStorage?.setCredential(cred, forProtectionSpace: space)
  83. //let session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
  84.  
  85. // let basicAuthCred = ("Basic share@rose-hulman.edu:share")
  86. // request.setValue(basicAuthCred, forHTTPHeaderField:"Authorization")
  87.  
  88. var confirm : NSDictionary?
  89. let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
  90. println("Registration Task completed")
  91. if(error != nil) {
  92. // If there is an error in the web request, print it to the console
  93. println(error.localizedDescription)
  94. }
  95. var err: NSError?
  96. println(response)
  97. var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
  98. confirm = jsonResult
  99. println(jsonResult)
  100.  
  101. dispatch_async(dispatch_get_main_queue(), {
  102. self.navigationController?.popViewControllerAnimated(true)
  103. return
  104. })
  105. })
  106. task.resume();
  107. var emailRegister = UIAlertView(title: "Email Confirmation", message: "An account confirmation email has been sent to your email", delegate: nil, cancelButtonTitle: "close")
  108. emailRegister.show();
  109.  
  110. //SPECIAL CALL TO CONFIRM Email
  111. //NEEDS TO BE REMOVED ONCE EMAIL CONFIRMATION IS WORKING
  112.  
  113. data = ["confirmation_token" : confirm["confirm"]]
  114. urlPath = "http://datass.io:5006/confirm_email" //"http://137.112.104.213:8000/users/"
  115. url = NSURL(string: urlPath)
  116. request = NSMutableURLRequest(URL: url)
  117.  
  118. session = NSURLSession.sharedSession() //NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
  119. request.HTTPBody = jsonData
  120. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  121. request.addValue("application/json", forHTTPHeaderField: "Accept")
  122. request.HTTPMethod = "POST"
  123.  
  124. let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
  125. println("Confirmation Task completed")
  126. if(error != nil) {
  127. // If there is an error in the web request, print it to the console
  128. println(error.localizedDescription)
  129. }
  130. var err: NSError?
  131. println(response)
  132. var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
  133. confirm = jsonResult
  134. println(jsonResult)
  135.  
  136. dispatch_async(dispatch_get_main_queue(), {
  137. self.navigationController?.popViewControllerAnimated(true)
  138. return
  139. })
  140. })
  141. task.resume();
  142.  
  143. }
  144.  
  145.  
  146. }
  147. @IBAction func comparePassword(AnyObject){
  148. if(passTextField.text != cpassTextField.text){
  149. passConfValidation.hidden = false;
  150. }
  151. else {
  152. passConfValidation.hidden = true;
  153. }
  154. if countElements(cpassTextField.text)>512 || countElements(cpassTextField.text)<6 {
  155. cpassValidation.hidden = false;
  156. }
  157. else {
  158. cpassValidation.hidden = true;
  159. }
  160.  
  161. }
  162. @IBAction func emailValid(AnyObject){
  163.  
  164. }
  165. @IBAction func usernameLength(AnyObject){
  166. if countElements(nameTextField.text)>512 || countElements(nameTextField.text)<1 {
  167. nameValidation.hidden = false;
  168. }
  169. else {
  170. nameValidation.hidden = true;
  171. }
  172.  
  173. }
  174. @IBAction func passwordLength(AnyObject){
  175. if countElements(passTextField.text)>512 || countElements(passTextField.text)<6 {
  176. passValidation.hidden = false;
  177. }
  178. else {
  179. passValidation.hidden = true;
  180. }
  181. }
  182.  
  183.  
  184. override func viewDidLoad() {
  185. super.viewDidLoad()
  186. // Do any additional setup after loading the view, typically from a nib.
  187. }
  188.  
  189. override func didReceiveMemoryWarning() {
  190. super.didReceiveMemoryWarning()
  191. // Dispose of any resources that can be recreated.
  192. }
  193. @IBAction func backgroundTap(sender: AnyObject){
  194. self.view.endEditing(true)
  195. }
  196. override func viewWillAppear(animated: Bool) {
  197. super.viewWillAppear(animated)
  198. if let obj: AnyObject = dataObject {
  199. self.dataLabel!.text = obj.description
  200. } else {
  201. self.dataLabel!.text = ""
  202. }
  203. }
  204.  
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement