I am new to Swift. I built a simple application which works fine on the simulator. I am running the same on my device (iPhone 6s with iOS 11.0.2) and it's failing to connect to server.
getting these errors:
2017-10-26 18:16:02.489134-0400 myproj[1451:206438] TIC TCP Conn Failed [1:0x1c0176800]: 1:61 Err(61)
2017-10-26 18:16:02.489771-0400 myproj[1451:206438] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> HTTP load failed (error code: -1004 [1:61])
2017-10-26 18:16:02.490293-0400 myproj[1451:206440] Task <0C30ADDC-4A0E-4815-A701-2EF0A7CF5F04>.<1> finished with error - code: -1004
Please help me understand this error.
EDIT:
Here is the code making that call to the server:
func postRequest(postData: NSDictionary, postHeaders: NSDictionary, endPoint: String,
onComplete: #escaping ((NSDictionary)->Void), callbackParams: NSDictionary = NSMutableDictionary()) {
let url:URL = baseUrl.appendingPathComponent(endPoint)
let session = URLSession.shared
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
var paramString = ""
for (key, value) in postData{
paramString = paramString + (key as! String) + "=" + (value as! String) + "&"
}
request.allHTTPHeaderFields = postHeaders as? [String : String]
request.httpBody = paramString.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data, let _:URLResponse = response , error == nil else {
return}
let json: Any?
do {
json = try JSONSerialization.jsonObject(with: data!, options: [])
}
catch {
return
}
var serverResponse = json as? NSDictionary
DispatchQueue.main.async{
for (key, value) in serverResponse!{
callbackParams.setValue(value, forKey: key as! String)
}
onComplete(callbackParams)
}
})
task.resume()
}
EDIT:
Thanks!
Error -1004 is URLError.cannotConnectToHost. It cannot connect to the server for some reason.
In comments, you say the URL is http://127.0.0.1. That is localhost, the current machine. If you use that URL on a physical phone, it's going to look for the web server on the phone. Lol. It works on the simulator, because the simulator is your computer, the localhost.
You need a URL that your iPhone can resolve to the machine running your web service. For example, find out what the IP for the computer running the web service on your local network, make sure your iPhone is on wifi on the same network, and then use that unique IP number for your computer on the LAN (likely something more like 192.168.0.x).
I was getting the same problem when i was sending/ receiving socket messages! I am using Socket.io and Firebase for push and the error was at the node.js backend. The error was that the mongoose version was deprecated. Whenever this error arises at the iOS End, ask the node.js backend team to look for Red color crash logs. They will surely find the problem causing this error on frontend. Hope this helps !!
In my case i'm not changes the Local host IP address.
Example : #"http://Localhost/Host name/index.pnp/apiname?";
initially i'm used above URL, so i'm got the same error...
Later i changed in to Localhost to 190.177.0.22
Example : #"http://190.177.0.22/Host name/index.pnp/apiname?";
Another corner case if you using something like ngrok and it's still not working is the phone isn't connected to a service provider network or wifi.
In my case my main phone was working the second out of service phone was not.
Related
Trying to figure this one out, I'm stumped. When making a REST call to get json data back from a response (GET or POST, each should return data) I get back 0 bytes.
This is pre-serialization. The POST successfully creates a message on the backend, and the backend shows a response being sent; with charles proxy on, I've confirmed that there is a response with valid JSON data.
Any ideas why this would be failing ONLY in iOS? Postman/Charles proxy (from the iOS calls!) shows valid data in the response, but the debugger picks up nothing.
Thanks in advance for anything thoughts.
let components = URLComponents(string: "mysuperValidURL.com")
guard let url = components?.url else {
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
setUrlRequestToken(request: &request)
let message = ChatMessage(content: message, group: group, userId: userId)
let jsonEncoder = JSONEncoder()
guard let data = try? jsonEncoder.encode(message) else {
return
}
URLSession.shared.uploadTask(with: request, from: data) { (data, response, error) in
// Here there be 0 bytes
}.resume()
}
Data will sometimes come back as 0 bytes in the debugger; add a print with debug description to ensure you're getting data. In this case it was a failure of the debugger mixed with a later serialization error that caused it to appear to be broken.
TLDR; don't trust the realtime debugger, use some prints to sanity check.
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 am getting this error when using SwiftyBeaver logger, which tries to send data to the cloud via this code:
func sendToServerAsync(str: String?, complete: (ok: Bool, status: Int) -> ()) {
if let payload = str, let queue = self.queue {
// create operation queue which uses current serial queue of destination
let operationQueue = NSOperationQueue()
operationQueue.underlyingQueue = queue
let session = NSURLSession(configuration:
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: nil, delegateQueue: operationQueue)
// assemble request
let request = NSMutableURLRequest(URL: serverURL)
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
// basic auth header
let credentials = "\(appID):\(appSecret)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentials.base64EncodedStringWithOptions([])
request.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")
// POST parameters
let params = ["payload": payload]
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
toNSLog("Error! Could not create JSON for server payload. \(error)")
}
//toNSLog("sending params: \(params)")
//toNSLog("\n\nbefore sendToServer on thread '\(threadName())'")
sendingInProgress = true
// send request async to server on destination queue
let task = session.dataTaskWithRequest(request) {
_, response, error in
var ok = false
var status = 0
//toNSLog("callback of sendToServer on thread '\(self.threadName())'")
if let error = error {
// an error did occur
self.toNSLog("Error! Could not send entries to server. \(error)")
} else {
if let response = response as? NSHTTPURLResponse {
status = response.statusCode
if status == 200 {
// all went well, entries were uploaded to server
ok = true
} else {
// status code was not 200
var msg = "Error! Sending entries to server failed "
msg += "with status code \(status)"
self.toNSLog(msg)
}
}
}
return complete(ok: ok, status: status)
}
task.resume()
}
}
The strange thing is it works for the first two or three log entries, and then stops due to the above error. I tried to reset content and settings on the simulator and reboot my simulator (as suggested in Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost.") but that just fixes it temporarily--after the first 2-3 log entries, it starts failing again.
I tried debugging this for hours with the creator of SwiftBeaver last night, but we couldn't get it to work. Seems like not many people are seeing this issue.
I tried removing my Wifi connection and reconnecting, but that didn't work either.
Any guidance on this would be much appreciated.
FYI, I'm using Swift 2 and XCode 7.3.
This is probably caused by HTTP keep-alive support being seriously buggy in the iOS simulator. See:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
for more details, but the short answer is to disable keep-alive on the server that you use when doing simulator testing, or better yet, add some logic that immediately retries the request if it sees that particular error.
I'm working on a iOS project in Swift 2.0, which has Web service calls, these services are slow to respond and that is normal, can be up to 1 minute or a little more, when i call the service 70% of the time it answers with the error "the network connection was lost." The tests were conducted in both simulator and different phone devices and iPad and the result is the same. The network connection is strong and the same application was also created on Android and working properly almost 100% of the time.
The way I call services from any view is as follows:
#IBAction func contratarAct(sender: AnyObject) {
conexion.delegate = self
loadingView = MEXLoadingView(delegate: self, title: "Espere por favor", percent: false, view: self.view)
self.loadingView.showAnimated(true)
let url = urlServicios.urlBaseServicios + "/" + idSolicitud + "/" + idNoCliente + "/CONTRATO"
conexion.consultaServicioGET(url, httpMethod: "PUT")
}
And the method that is executed is as follows:
func consultaServicioGET(url : String, httpMethod : String ){
let urlString = url
let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
request.timeoutInterval = 540
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
var session = NSURLSession.sharedSession()
request.HTTPMethod = httpMethod
let urlconfig = NSURLSessionConfiguration.defaultSessionConfiguration()
urlconfig.timeoutIntervalForRequest = 540
urlconfig.timeoutIntervalForResource = 540
session = NSURLSession(configuration: urlconfig, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithRequest(request , completionHandler: {
(data:NSData?, response:NSURLResponse?, error:NSError?) in
if error != nil {
let jsonError : NSDictionary = NSDictionary()
self.delegate?.respuestaServicioGET!(jsonError, mensaje: "\(error!.localizedDescription)")
return
}
let jsonString = NSString(data: data!,encoding: NSASCIIStringEncoding)
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
let json: NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
if (json.isKindOfClass(NSDictionary) ){
self.delegate?.respuestaServicioGET!(json, mensaje: "OK")
}else{
let jsonError : NSDictionary = NSDictionary()
self.delegate?.respuestaServicioGET!(jsonError, mensaje: "ERROR")
}
})
task.resume()
}
the error displayed is:
error=Optional(Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x7fbde5f51df0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://particulares-gw-obparticularesmx-pre.appls.cto2.paas.gsnetcloud.com:443/OPB/57dadf7de4b0ac2e518de44a/57dadf7de4b06c6b04ef0dcf/CONTRATO, NSErrorFailingURLKey=https://particulares-gw-obparticularesmx-pre.appls.cto2.paas.gsnetcloud.com:443/OPB/57dadf7de4b0ac2e518de44a/57dadf7de4b06c6b04ef0dcf/CONTRATO, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.})
I add some code like the following:
urlconfig.timeoutIntervalForRequest = 540
urlconfig.timeoutIntervalForResource = 540
Trying to get more "timeout" but this is not looks like a timeout.
I can not get out of this error for days, any help will be greatly appreciated. I'm desperate.
If you're expecting a socket to stay open for minutes at a time, you're in for a world of hurt. That might work on Wi-Fi, but on cellular, there's a high probability of the connection glitching because of tower switching or some other random event outside your control. When that happens, the connection drops, and there's really nothing your app can do about it.
This really needs to be fixed by changing the way the client requests data so that the responses can be more asynchronous. Specifically:
Make your request.
On the server side, immediately provide the client with a unique identifier for that request and close the connection.
Next, on the client side, periodically ask the server for its status.
If the connection times out, ask again.
If the server says that the results are not ready, wait a few seconds and ask again.
On the server side, when processing is completed, store the results along with the identifier in a persistent fashion (e.g. in a file or database)
When the client requests the results for that identifier, return the results if they are ready, or return a "not ready" error of some sort.
Have a periodic cron job or similar on the server side to clean up old data that has not yet been collected.
With that model, it doesn't matter if the connection to the server closes, because a subsequent request will get the data successfully.
I faced the same issue and I am attaching a screenshot of the resolution to show how I resolved the issue.
In my case, the issue was that the API requests are blocked from the server Sucuri/Cloudproxy (Or you can say firewall service). Disabling the firewall resolved the issue
I don't why but it's works when I add sleep before my request:
sleep(10000)
AF.request(ViewController.URL_SYSTEM+"/rest,get_profile", method: .post, parameters: params, encoding: JSONEncoding.default , headers: headers).responseJSON { (response) in
}
I faced this issue and spend more than 1 week to fix this. AND i just solved this issue by changing Wifi connection.
I have submitted an app to iTunes and Apple rejected it because of a crash.
I symbolicated and analyzed the crashreport and saw that it crash at a json call.
I try to reproduce it and I found that it just happens when I turn off my wlan.
Does Apple test apps offline?
How can I handle this error? And make my jsoncall better.
This is my method:
var session = NSURLSession.sharedSession();
var uri = "/GetNews";
let request : NSMutableURLRequest = CreateRequest(uri, HTTPmethod: "GET");
let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil;
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? Dictionary<String, AnyObject>;
let resp : NewsResponse = NewsResponse(jsonData: jsonResult!);
completionHandler?(resp);
});
task.resume();
It crashs at let resp..., because jsonResult is nil and I use !
Of course Apple tests apps offline, and you should too. You should plan on every call that requires an internet connection failing, and you should handle every error appropriately.
The appropriately part is up to your app. For example, some apps (like Facebook) let you read posts you've already downloaded, and queue up posts you write to be sent when you get an internet connection. Some apps just don't work at all without an internet connection and it doesn't make sense for them to do anything but put up an error message (like, for example, a iTunes radio).
If your app is a news reader, perhaps the best thing to do is use a cache and let them read news they've downloaded in the past. A simple, unobtrusive message letting them know they're offline and new articles will be downloaded once they're back on would suffice; a crash, though, is very bad in terms of usability and utility.
TO FIX CRASH
As Eric stated, you should use "safe unwrapping", better known as optional binding. Try this:
var session = NSURLSession.sharedSession();
var uri = "/GetNews";
let request : NSMutableURLRequest = CreateRequest(uri, HTTPmethod: "GET");
let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil;
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? Dictionary<String, AnyObject>;
//if non-nil, assigns jsonResult to nonNilJsonResult
if let nonNilJsonResult = jsonResult {
let resp : NewsResponse = NewsResponse(jsonData: nonNilJsonResult!);
completionHandler?(resp);
}
});
task.resume();