Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
  2. {
  3. //Setup the NSURLSessionConfiguration
  4.  
  5. let configuration = URLSessionConfiguration.default
  6.  
  7. //Setup the NSURLSession
  8.  
  9. let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
  10.  
  11. //Add the username and password to NSURLCredential
  12.  
  13. credential = URLCredential(user:username, password:password, persistence: .forSession)
  14.  
  15. //Create request URL as String
  16.  
  17. let requestString = NSString(format:"%@", webservice) as String
  18.  
  19. //Convert URL string to NSURL
  20.  
  21. let url: URL! = URL(string: requestString)
  22.  
  23. //Prepare the task to get data.
  24.  
  25. let task = session.dataTask(with: url, completionHandler: {
  26. data, response, error in
  27.  
  28. DispatchQueue.main.async(execute: {
  29.  
  30. if(error == nil)
  31. {
  32.  
  33. //If there is no error calling the API, return true
  34.  
  35. completion(true)
  36. }
  37. else
  38. {
  39.  
  40. //If there is an error calling the API, return false
  41.  
  42. completion(false)
  43. }
  44.  
  45. })
  46.  
  47. })
  48.  
  49. //Run the task to get data.
  50.  
  51. task.resume()
  52.  
  53. }
  54.  
  55. fatal error: unexpectedly found nil while unwrapping an Optional value
  56.  
  57. let task = session.dataTask(with: url, completionHandler: {
  58. data, response, error in
  59.  
  60. DispatchQueue.main.async(execute: {
  61.  
  62. if(error == nil)
  63. {
  64.  
  65. //If there is no error calling the API, return true
  66.  
  67. completion(true)
  68. }
  69. else
  70. {
  71.  
  72. //If there is an error calling the API, return false
  73.  
  74. completion(false)
  75. }
  76.  
  77. })
  78.  
  79. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement