I've been writing an app with Swift 4 and it communicates with a service via RESTful API.
I have found some helpful code examples and everything works well as the service is running.
However, I could not handle the situation that the service is closed. I have tried try/catch blocks but they did not work well. Please check the code below firstly. It's the part of the communication. Basically it sends an email address to the service and gets a response as an activation code.
URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in
guard error == nil else {
// Some code
return
}
guard let responseData = data else {
// Some code
return
}
do {
guard let activation_response = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
// Some code
return
}
guard let activation_code = activation_response["activation_code"] as? String else {
// Some code
return
}
// Some code, here, if it works fine.
} catch {
// Some code
return
}
}.resume()
When the service is closed, simply it will throw an error such as TCP connection failed. How can I handle it?
The error I get is something like below:
TheApp[4184:399699] TIC TCP Conn Failed [1:0x604000166600]: 1:61 Err(61)
2018-01-02 20:35:04.930874+0300 TheApp[4184:399699] Task <5ED49B16-BFDC-45A5-85F5-095C4FC4D84D>.<1> HTTP load failed (error code: -1004 [1:61])
2018-01-02 20:35:04.934352+0300 TheApp[4184:399698] Task <5ED49B16-BFDC-45A5-85F5-095C4FC4D84D>.<1> finished with error - code: -1004
It throws an exception and I can not catch it. I could not find how to do it. Thank you very much in advance for your help.
Related
I'm using swiftHTTP for requesting to my server and when my internet connection is slow, it goes to response part! I've set the example code below:
HTTP.GET("myURL") { response in
let myResponse = response.data // it comes here after the timeout
if response.statusCode == 200 {
//some code
} else {
do {
let jsonError = try JSON(data: myResponse) // in this line it goes to catch because there is no data in myresponse
} catch{
//alert for not having connection
}
}
Why does it call the response function if there's no response?
My server also says, that no request was sent.
It doesn't "go to response", it tries to make the HTTP request as expected and regardless of success or error it's completion handler is called.
The response object that is returned is an object that contains all of the information you need to determine what happened with the request.
So it will contain a timeout status code (HTTP 408), possibly an error message. If it did not respond at all, your app would not be able to handle these cases.
Say for example your user taps on their profile icon in the app and this sends a request to get the users profile, but it timed out. Do you want the user sat waiting, looking at a blank profile screen? It's much better to catch the error and handle it gracefully. In this case you could present a message to the user telling them that something went wrong and close the empty profile screen
Your response handler will also be called from swiftHTTP, if there's no or a very bad connection.To solve this problem, either check if there is an internet connection or check if the data is nil:
HTTP.GET("myURL") { response in
let myResponse = response.data // it comes here after the timeout
if response.statusCode == 200 || response.data == nil {
//some code
} else {
do {
let jsonError = try JSON(data: myResponse) // in this line it goes to catch because there is no data in myresponse
} catch{
//alert for not having connection
}
}
The important part here is the check if response.data == nil.
I have implemented the NEHotspotHelper so that I can perform authentication in the background for networks with a captive portal.
I need to perform a web request in the 'authenticating' state so that I can retrieve the Wispr and also access an API.
However, when I try to use URLSession to send a web request, the request fails. This is the error:
[594:88737] dnssd_clientstub read_all(7) DEFUNCT
[594:88783] TIC TCP Conn Failed [4:0x1c0177a00]: 12:8 Err(-65554)
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> HTTP load failed (error code: -1003 [12:8])
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> finished with error - code: -1003
See a snippet of my code:
let registered = NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
print("Received command: \(cmd.commandType.rawValue)")
if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
//Get all available hotspots
print("filter scan list")
var list: [NEHotspotNetwork] = cmd.networkList!
var response: NEHotspotHelperResponse
for l in list {
if (l.ssid=="my-ssid") {
response = cmd.createResponse(NEHotspotHelperResult.success)
} else {
response = cmd.createResponse(NEHotspotHelperResult.failure)
}
response.setNetworkList([chosenNetwork])
response.deliver()
}
} else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
if let network = cmd.network {
if (network.ssid=="my-ssid") {
network.setConfidence(NEHotspotHelperConfidence.high)
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetwork(network)
response.deliver() //Respond back
} else {
let response = cmd.createResponse(NEHotspotHelperResult.failure)
response.deliver()
}
}
} else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
print("authenticate")
var response = cmd.createResponse(NEHotspotHelperResult.unsupportedNetwork)
if let network = cmd.network{
if network.ssid == "my-ssid"{
self.queryUrl()
response = cmd.createResponse(NEHotspotHelperResult.success)
}
}
response.deliver() //Respond back
}
}
func queryUrl(){
let config = URLSessionConfiguration.default
config.allowsCellularAccess = false;
let session = URLSession.init(configuration: config)
let url = URL(string: "https://172.217.20.35")
let semaphore = DispatchSemaphore(value: 0)
let task = session.dataTask(with: url!){(data, response, error) in
if data==nil {
print(data as Any)
}
else{
print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as Any)
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .distantFuture)
}
I was also facing the similar issue. However, I found that developer need to bind the request with the received command before making web request to the connected network. All you need to do is to make NSMutableURLRequest and then call hitTestURLRequest.bind(to: command) because bind function is defined in the category of NSMutableURLRequest.
Others have reported problem with name server resolution from NEHotspotHelper callback. Try using an IP address to make the call.
Also don't forget URLSession works asynchronously. You will need to call response.deliver() only after the web service calls finishes.
This is the just of it:
if cmd.commandType
filterScanList
- SetConfidence to each of your networks in cmd.networkList - High and add to a local list variable.
- create Response.Success
- set Response.NetworkList to your list of confident networks
----
- deliver Response
Evaluate or PresentUI
//If it's your network
- SetConfidence to cmd.network - High
- create Response.Success
- set Response.Network to cmd.network
//If it's not your network
- create Response.UnsupportedNetwork
----
- deliver Response
Authenticate or Maintain
//If it's your network
- SetConfidence to cmd.network - High
- create Response.Success
- set Response.Network to cmd.network
- TRY AUTHENTICATE HERE SYNCHRONOUSLY
//If it's not your network
- create Response.UnsupportedNetwork
----
- deliver Response
Logoff or None
- create Response.Success
----
- deliver Response
Im trying to connect to a ruby sinatra server that im running locally on my mac from an app using the following code:
func load(finished: #escaping ()->()) {
// Create destination URL
let destinationFileUrl = documentsUrl.appendingPathComponent("Images.zip")
//Create URL to the source file you want to download
let fileURL = URL(string: "http://waynerumble.local~waynerumble:4567/download")
//Create Session
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
}
finished()
} else {
print("Error took place while downloading a file. Error description: %#", (error?.localizedDescription)! as String);
finished()
}
}
task.resume()
}
If i test the app from the simulator and set the fileURL to "http://127.0.0.1:4567/download" it works fine but from device i understand this has to be different so far I've tried:
From running ifconig in terminal i get 192.168.1.254 at en1 so i tried "http://192.168.1.255:4567/download" which gave me:
[] nw_socket_connect connectx failed: [13] Permission denied
Error took place while downloading a file. Error description: %# Could not connect to the server.
Ive also tried:
"http://waynerumble.local:4567/download" which gives:
Error took place while downloading a file. Error description: %# Could not connect to the server.
"http://waynerumble.local.~waynerumble:4567/download"(waynerumble is my computer name and username) which gives:
Error took place while downloading a file. Error description: %# A server with the specified hostname could not be found.
I also have wifi internet sharing on from both ethernet and iphone. Im not sure what else to try
192.168.1.255 is a brodcast adress for your network and you should not use it.
Why dont you connect to your real IP 192.168.1.254?
To bind Sinatra app to every interface try:
class MyApp < Sinatra::Base
set :bind, '0.0.0.0'
Then http://192.168.1.254:4567/download should work.
Also remember about opening desired port in the firewall.
I deployed my code before Swift 2 came out and with Crashlytics I get about 2-3 crashes a day supposedly in this func. I can't, for the life of me, figure out what's going on. I have thousands of users but out of the myriad that use the app I end up getting about 8-10 a day where Crashlytics reports something wrong.
The error is:
_TFZFC11 GProxy8GetAsyncFMS0_U_S_14Deserializable__FTGCS_10
GRequestQ__8callbackFGCS_11 GResponseGSaQ0___
T__T_U_FTGSQCSo6NSData_GSQCSo13 NSURLResponse_GSQCSo7 NSError__T_
(GProxy.swift:141)
I am making a request to my server and processing an array back and doing something with it in my callback (further up the chain somewhere else in my app).
Here is the code:
class func GetAsync<R, T: Deserializable>(request: GRequest<R>, callback: (GResponse<Array<T>>) -> ())
{
var list = Array<T>()
var response = GResponse<Array<T>>()
let serverRequest = NSMutableURLRequest(URL: NSURL(string: API_URL + request.Url)!)
serverRequest.HTTPMethod = "GET"
let task = NSURLSession.sharedSession().dataTaskWithRequest(serverRequest,
completionHandler: {
data, r, error in
if error != nil {
response.Status = .ERROR
} else {
do
{
let responseArray = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSArray
for item: AnyObject in responseArray {
let row = T(dict: item as! NSDictionary)
list.append(row)
}
response = GResponse<Array<T>>(status: ResponseCode.OK, value: list)
}
catch let err as NSError{
//I now log here (this is new code, before I was not)
}
}
callback(response)
})
task.resume()
}
Are there any major faux pas here where a crash may occur? See any glaring errors or potential concerns? I don't have the actual source anymore of when I deployed (I made changes to this class so I don't recall what line 141 is, but the crash message seems to do with NSURLResponse and the like. Any thoughts?
Thanks so much!
I have this function that takes care of an API call (makeAPICall) that I'd like to throw an error for certain API responses and when the httpResponse.statusCode != 200.
The problem is that, as far as I know, NSURLSession().dataTaskWithRequest(...) can't throw. Is this correct and if so, is there some workaround? Or should I do something totally different?
Since dataTaskWithRequest is an asynchronous operation, its error handling is facilitated with a completion handler. If it were to throw, it would be difficult to handle an error at the completion of the operation.
Therefore, you should handle the error condition within the completion handler. If you wanted to throw your own error upon completion, that would be possible but somewhat superfluous.
Instead of throwing an error, have your clients pass in a block, and run that block whenever the request fails (or, for that matter, when it completes successfully).
Actually you you can handle the error if occurs. For instance
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
// do whatever you want, there is an error
}
if let data = data{
print("data =\(data)")
}
if let response = response {
print("url = \(response.URL!)")
print("response = \(response)")
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
and I showed you how to get the response code as well.