Getting a value from web call in Swift [duplicate] - ios

This question already has answers here:
Returning data from async call in Swift function
(13 answers)
Closed 4 years ago.
I am trying to get the returned value from a PHP script in Swift. However, it seems as though I keep getting the error:
Unexpectedly found nil while unwrapping an Optional value
Here is the class:
var value: String!
func run(idNumber: Int) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
//answer = error;
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
}
task.resume()
}
func getValue() -> String{
return value
}
The error occurs when calling the getValue() function. However, when I print out the "answerString" as soon as it is created, it prints out successfully!
The functions are called here:
let access = ApiAccess()
access.run(idNumber: 0)
print(access.getValue())

Making a request is an asynchronous task. You need to wait the closure callback to be call before calling getValue.
You can add a closure callback to your run method. That way you will know when the request has finished and you can print the result:
var value: String!
func run(idNumber: Int, #escaping callback: () -> Void) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
//answer = error;
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
callback()
}
task.resume()
}
func getValue() -> String{
return value
}
let access = ApiAccess()
access.run(idNumber: 0) {
print(access.getValue())
}

The issue is that the callback for URLSession.shared.dataTask() happens asynchronously, so you'll end up executing access.getValue() before your var value is ever set. This means that value is forcefully unwrapped before it has a value, which causes this error.
To workaround this, consider using promises, RxSwift, or similar async tools so that you only access values when available.

Refactor your run(idNumber:) function to take a completion handler:
func run(idNumber: Int, completion: (String?, Error?)-> Void ) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
completion(nil, error)
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
completion(answerString, nil)
}
task.resume()
}
And call it like this:
let access = ApiAccess()
access.run(idNumber: 0) { result, error in
guard let result = result else {
print("No result. Error = \(error)")
return
}
print("result = \(result)")
}
(Or use Futures or Promises, as mentioned by #JohnEllmore in his answer)

Related

Swift - Why doesn't this find the variable (let)? [duplicate]

This question already has answers here:
Swift closure async order of execution
(1 answer)
Returning data from async call in Swift function
(13 answers)
Returning method object from inside block
(3 answers)
Closed 5 years ago.
func isEmailTaken(email:String) -> String {
let myUrl = URL(string: "URL");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
let postString = "email=\(email)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let emailAlreadyTakenData = parseJSON["emailAlreadyTaken"] as! String
print(emailAlreadyTakenData)
}
} catch {
print(error)
}
}
task.resume()
return(emailAlreadyTakenData)
}
The line :
return(emailAlreadyTakenData)
Doesnt get the variable value. So, the http request gets the data successfully but the return command doesn't parse the correct data.
Your variable is in different scope, then when you declared it. So you cannot access the variable outside of the defined scope

Get data from URL as separate function [duplicate]

This question already has answers here:
function with dataTask returning a value
(4 answers)
Closed 5 years ago.
I try to create function to get data from URL:
func getStringFromUrl(urlString: String) -> String {
if let requestURL = URL(string: urlString) {
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: requestURL, completionHandler: { (data, response, error) in
if let data = data {
do {
let str = String(data: data, encoding: String.Encoding.utf8)
return str
}
catch let error as NSError {
print ("error = \(error)")
}
}
else {
print ("error = \(error)")
}
})
task.resume()
}
}
But I got this error: unexpected non-void return value in void function
How can I create a separate function to get data from Url?
In your code you have:
let str = String(data: data, encoding: String.Encoding.utf8)
return str
Which is inside a closure block which is not defined to return anything. Because the function session.dataTask is an asynchronous task, it won't return straight away. You should use a completion block/closure to get the response when it returns. Also bear in mind that it might not return, so the string needs to be optional. See the code below.
func getStringFromUrl(urlString: String, completion: #escaping (_ str: String?) -> Void) {
if let requestURL = URL(string: urlString) {
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: requestURL, completionHandler: { (data, response, error) in
if let data = data {
do {
let str = String(data: data, encoding: String.Encoding.utf8)
completion(str)
}
catch let error as NSError {
print ("error = \(error)")
completion(nil)
}
}
else {
print ("error = \(error)")
completion(nil)
}
})
task.resume()
}
}
EDIT: Usage
getStringFromUrl(urlString: "http://google.com") { str in
if let text = str {
// you now have string returned
}
}

Swift - Multiple Parameters to webservice

I have the following code that should send a username and password off to a webservice, in return I get a single integer back:
func attemptLogin() {
let url:URL = URL(string: endpoint+"/LoginNew")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let postString = "username="+txtUsername.text! + "; password="+txtPassword.text!
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let data = data, let _:URLResponse = response, error == nil else {
print("error")
return
}
let dataString = String(data: data, encoding: String.Encoding.utf8)
print(dataString)
}
task.resume()
}
In my function I need to add two parameters are I'm trying to do in this line:
let postString = "username="+txtUsername.text! + "; password="+txtPassword.text!
request.httpBody = postString.data(using: String.Encoding.utf8)
I am getting the following response from my web service when I run this however
Optional("Missing parameter: password.\r\n")
I am obviously not appending the parameters to the request properly but I'm not sure what I've done wrong.
It is good practice to avoid using explicit unwraps of optionals (using !), use guard let for text i UITextFields instead.
And why not separate into two methods, attemptLogin and login, which maybe can take a closure for code to execute when sign in completed? Maybe the closure can take an Result enum.
Like this:
typealias Done = (Result) -> Void
enum MyError: Error {
case unknown
}
enum Result {
case success(String)
case failure(MyError)
init(_ error: MyError) {
self = .failure(error)
}
init(_ dataString: String) {
self = .success(dataString)
}
}
func login(username: String, password: String, done: Done? = nil) {
let session = URLSession.shared
guard
let url = URL(string: endpoint+"/LoginNew"),
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let postString = "username=\(username)&password=\(password)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request) {
(data, response, error) in
guard let data = data else { done?(Result(.unknown)); return }
let dataString = String(data: data, encoding: String.Encoding.utf8)
done?(Result(dataString))
}
task.resume()
}
func attemptLogin() {
guard
let username = txtUsername.text,
let password = txtPassword.text
else { return }
login(username: username, password: password) {
result in
swicth result {
case .success(let dataString):
print(dataString)
case .failure(let error):
print("Failed with error: \(error)")
}
}
}
Disclaimer: Have not tested the code above, but hopefully it compiles (at least with very small changes).

Swift - Return data from NSURLSession when sending POST request

I can send a POST request in Swift using the below code
func post() -> String{
let request = NSMutableURLRequest(URL: NSURL(string: "http://myserverip/myfile.php")!)
request.HTTPMethod = "POST"
let postString = "data=xxxxxxx"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString!)")
}
task.resume()
return "";//how would i return data here
}
I need to return the result, but this isn't possible since the network request is asynchronous. I think I can use a listener to wait for the result and then return it, but I'm not sure how this would work or how to implement it
If anyone could help, I would greatly appreciate it
I am new to both iOS and Swift
Your post function might be like:
func post(completionHandler: (response: String) -> ()) { your code }
And in the response part:
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
completionHandler(response: responseString)
Finally, you can call your post method like:
post( {(response: String) -> () in
println("response = \(response)")})
You can use a delegate or pass a function as an argument of post that you call at the end
Example with a completion handler :
struct RemoteCenter {
static func post(completion: (message: String?) -> Void) {
let request = NSMutableURLRequest(URL: NSURL(string: "http://myserverip/myfile.php")!)
request.HTTPMethod = "POST"
let postString = "data=xxxxxxx"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
completion(message: NSString(data: data, encoding: NSUTF8StringEncoding) as? String)
}
task.resume()
}
}
class YourController: UIViewController {
override func viewDidLoad() {
RemoteCenter.post(completionHandler)
}
func completionHandler(message: String?){
println("I got \(message)")
}
}
Example with a delegate :
protocol DelegateProtocol {
func didReceiveAMessage(message: String?)
}
struct RemoteCenter {
var delegate:DelegateProtocol?
func post() {
let request = NSMutableURLRequest(URL: NSURL(string: "http://myserverip/myfile.php")!)
request.HTTPMethod = "POST"
let postString = "data=xxxxxxx"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
self.delegate?.didReceiveAMessage(NSString(data: data, encoding: NSUTF8StringEncoding) as? String)
}
task.resume()
}
}
class YourController: UIViewController, DelegateProtocol {
var remote = RemoteCenter()
override func viewDidLoad() {
remote.delegate = self
remote.post()
}
func didReceiveAMessage(message: String?) {
println("I got \(message)")
}
}
In both cases, the goal is to fetch some data on the internet from an UIViewController and print the message when it's done.
You can find so many tutorials about it. Check http://www.raywenderlich.com

NSURLConnection sendAsynchronousRequest can't get variable out of closure

I'm trying to get a simple text response from a PHP page using POST. I have the following code:
func post(url: String, info: String) -> String {
var URL: NSURL = NSURL(string: url)!
var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
var output = "Nothing Returned";
request.HTTPMethod = "POST";
var bodyData = info;
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
response, data, error in
output = (NSString(data: data, encoding: NSUTF8StringEncoding))!
}
return output
}
While this code does not throw any errors, when I make a call to it like this:
println(post(url, info: data))
It only prints: "Nothing Returned" even though if I were to change the line:
output = (NSString(data: data, encoding: NSUTF8StringEncoding))!
to this:
println((NSString(data: data, encoding: NSUTF8StringEncoding)))
it does print out the proper response. Am I doing something wrong with my variables here?
This is calling asynchronous function that is using a completion handler block/closure. So, you need to employ the completion handler pattern in your own code. This consists of changing the method return type to Void and adding a new completionHandler closure that will be called when the asynchronous call is done:
func post(url: String, info: String, completionHandler: (NSString?, NSError?) -> ()) {
let URL = NSURL(string: url)!
let request = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
let bodyData = info
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { response, data, error in
guard data != nil else {
completionHandler(nil, error)
return
}
completionHandler(NSString(data: data!, encoding: NSUTF8StringEncoding), nil)
}
}
Or, since NSURLConnection is now formally deprecated, it might be better to use NSURLSession:
func post(url: String, info: String, completionHandler: (NSString?, NSError?) -> ()) -> NSURLSessionTask {
let URL = NSURL(string: url)!
let request = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
let bodyData = info
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
dispatch_async(dispatch_get_main_queue()) {
guard data != nil else {
completionHandler(nil, error)
return
}
completionHandler(NSString(data: data!, encoding: NSUTF8StringEncoding), nil)
}
}
task.resume()
return task
}
And you call it like so:
post(url, info: info) { responseString, error in
guard responseString != nil else {
print(error)
return
}
// use responseString here
}
// but don't try to use response string here ... the above closure will be called
// asynchronously (i.e. later)
Note, to keep this simple, I've employed the trailing closure syntax (see Trailing Closure section of The Swift Programming Language: Closures), but hopefully it illustrates the idea: You cannot immediately return the result of an asynchronous method, so provide a completion handler closure that will be called when the asynchronous method is done.

Resources