Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
- {
- //Setup the NSURLSessionConfiguration
- let configuration = URLSessionConfiguration.default
- //Setup the NSURLSession
- let session = Foundation.URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
- //Add the username and password to NSURLCredential
- credential = URLCredential(user:username, password:password, persistence: .forSession)
- //Create request URL as String
- let requestString = NSString(format:"%@", webservice) as String
- //Convert URL string to NSURL
- let url: URL! = URL(string: requestString)
- //Prepare the task to get data.
- let task = session.dataTask(with: url, completionHandler: {
- data, response, error in
- DispatchQueue.main.async(execute: {
- if(error == nil)
- {
- //If there is no error calling the API, return true
- completion(true)
- }
- else
- {
- //If there is an error calling the API, return false
- completion(false)
- }
- })
- })
- //Run the task to get data.
- task.resume()
- }
- fatal error: unexpectedly found nil while unwrapping an Optional value
- let task = session.dataTask(with: url, completionHandler: {
- data, response, error in
- DispatchQueue.main.async(execute: {
- if(error == nil)
- {
- //If there is no error calling the API, return true
- completion(true)
- }
- else
- {
- //If there is an error calling the API, return false
- completion(false)
- }
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement