NSURLConnection sendSynchronousRequest does not work - ios

Any idea why sendSynchronousRequest not work, and returns nil. Error and response is nil as well.
let url = NSURL(fileURLWithPath: "http://google.com")!
let ur = NSMutableURLRequest(URL: url)
let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
let errorPtr = NSErrorPointer()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let data = NSURLConnection.sendSynchronousRequest(ur, returningResponse: response, error: errorPtr)
if errorPtr != nil {
var error: NSError = errorPtr.memory!
}
})
UPDATE
I tried in the async way:
var oq = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(ur, queue: oq, completionHandler: {response, data, error in
let ii = 7
})
Here I get an error:
(lldb) po error
Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x1567f9d0 {NSErrorFailingURLStringKey=file:///http:/google.com, NSErrorFailingURLKey=file:///http:/google.com, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x15691880 "The requested URL was not found on this server."}
Strange google is not available?

You are using fileURLWithPath and that will return the URL
file:///private/var/folders/xs/6k9v2g217155wr9vgb3xrg_80000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-F81D60A5-6797-4BEB-8AB9-2D156E2B6771/http:/google.com
What you need is let url = NSURL(string: "http://google.com")!
and that will return the URL:
http://google.com

Related

swift http request can't get non-200 response

I am using below swift code to make a http request. If the server response 200 OK response, swift can work properly. However, the completionHandler callback never get called until timeout when the server response other than 200 status. Is there any other setting on swift to prevent receiving other status response?
let urlStr = "http://xxxxx"
let url = NSURL(string: urlStr)!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "post"
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {(data, response, error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
// no response code
print("response")
return
}
print("get response from register service \(data) \(error)")
})
task.resume()
If that happens, I get timeout error below:
get login response nil error:Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=http://XXXXX, NSErrorFailingURLKey=http://localhost:8080/nurse/login, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2103, NSLocalizedDescription=The request timed out.})
Also I tested with curl command and I was able to get the response as below. So the server side works fine for me. I believe the problem is on the client side.
$ curl -i -X POST -d 'mobile=1234' http://localhost:8080/nurse/login
HTTP/1.1 10029
Server: Apache-Coyote/1.1
X-Application-Context: application
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: access_token
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Thu, 24 Mar 2016 02:39:09 GMT
This seems like a server side problem. The code below works (in Playground):
Try using this url and see if this is happening for you.
Also, I would use a simple tool like curl to test manually your server side before debugging Swift code:
curl -i -X POST -d 'name=yoyo' http://putsreq.com/vccZnwiNpP23RF7nwOPE
EDIT
Here is a function that does what you want. I hardcoded your URL and things are working now.
func postSomeStuff(data: String, completion: (data: NSData?, error: NSError?)->Void) {
let post: NSString = "data=\(data)"
let url = NSURL(string: "http://123.56.184.207:8080/nurse/login")!
let postData = post.dataUsingEncoding(NSUTF8StringEncoding)!
let postLength = String(postData.length)
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("*/*", forHTTPHeaderField: "Accept")
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.timeoutIntervalForRequest = 120
config.timeoutIntervalForResource = 240
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) { urlData, response, reponseError in
if let receivedData = urlData {
let res = response as! NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
if 200..<300 ~= res.statusCode {
completion(data: receivedData, error: nil)
} else {
let returnedError = NSError(domain: "getSomeStuff", code: 1, userInfo: [:])
completion(data: nil, error: returnedError)
}
} else {
var userInfo: [NSObject: AnyObject] = [:]
if let error = reponseError {
userInfo["message"] = error.localizedDescription
userInfo["cause"] = error
}
let returnedError = NSError(domain: "myFunc", code: 2, userInfo: userInfo)
completion(data: nil, error: returnedError)
}
}
task.resume()
}
postSomeStuff("yoyo") { data , err in
if let data = data { print(data) }
if let err = err { print(err) }
}
let sema = dispatch_semaphore_create(0);
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
// Used only in Playground
XCPSetExecutionShouldContinueIndefinitely(true)

iOS Basic Auth completion error first time

I have implemented this piece of code for basic auth and sometimes doesn't work on first time:
func doRequestWithBasicAuth(completion : (success : Bool, html: String?, error : NSError?) -> Void) {
if let user = self.user {
let loginString = NSString(format: "%#:%#", user.login!, user.pwd!)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions(nil)
let url = NSURL(string: user.service!.getURL())
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
if error == nil {
let htmlString = NSString(data: data, encoding: NSUTF8StringEncoding)
completion(success: true, html: htmlString as? String, error: nil)
} else {
completion(success: false, html: nil, error: error)
}
}
} else {
completion(success: false, html: nil, error: NSError())
}
}
This method is attached to a button, so on first tap I get this error:
BASIC AUTH ERROR: Error Domain=NSURLErrorDomain Code=-1012 "The
operation couldn’t be completed. (NSURLErrorDomain error -1012.)"
UserInfo=0x7bad5770
{NSErrorFailingURLKey=https:///auth/Logon.do,
NSErrorFailingURLStringKey=https:///auth/Logon.do,
NSUnderlyingError=0x7b9c47f0 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -1012.)"}
On second tap, call works fine... I can't understand why.. I print user login, psw and url: are the same on twice calls..
Any idea?

kCLErrorDomain iOS with request interrupted sock=client heroku

I have a backend hosted on heroku using django and I have an iOS app consuming that api. Sometimes the app throws this error
locman err: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
The backend for that same request throws this error
sock=client at=error code=H18 desc="Request Interrupted" status=503
How should I handle this in iOS? It seems like its a problem with the app. Am I just sending up too many requests at once?
heres what my api controller looks like
class func getPictures(location_id: String, completionHandler:( cardImageArray: [CardImage] )->() ){
var bodyString: String = "location_id=\(location_id)"
bodyString = bodyString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let url: NSURL = NSURL(string: BASE_URL + "/api/v1/get_pictures" + "?" + bodyString)!
let req: NSMutableURLRequest = NSMutableURLRequest(URL: url)
req.HTTPMethod = "GET"
//req.HTTPBody = bodyString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
NSURLConnection.sendAsynchronousRequest(
req,
queue: NSOperationQueue.mainQueue())
{ (res: NSURLResponse!, data: NSData! , err: NSError!) -> Void in
let jsonDict = JSON(data: data, options: NSJSONReadingOptions.AllowFragments, error: nil)
if (jsonDict["response"].int == 1){
var cardImageArray: [CardImage] = []
if let photo_urls = jsonDict["photo_urls"].array{
// NSString(data: data, encoding: nil)
for photo_url_raw in photo_urls{
if let url_string = photo_url_raw.string{
let ci = CardImage(urlstring: url_string)
cardImageArray.append(ci)
}
}
return completionHandler(cardImageArray: cardImageArray)
}
}else{
println("error in get pictures")
if let errorMessage = jsonDict["error"].string{
return
}
}
}
}
Figured out this was the error message being printed when location manager can't decide what the users location is. Just handled the error and had the user click a button to try again.

iOS - Synchronous Request with Timeout failing

I have code that looks like this:
let urlPath: String = "https://github.com/zemirco/sf-city-lots-json/raw/master/citylots.json"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url,
cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad,
timeoutInterval: 1)
var response:NSURLResponse?;
var error:NSError?;
/*** the follow line is giving the error ***/
var data:NSData = NSURLConnection.sendSynchronousRequest(
request1,
returningResponse: &response,
error: &error)!
if( error != nil) {
println ("ERROR")
} else {
println(response)
self.config = (NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary)
}
println("Done with Synchronous Request")
If the request takes too long, there's a fatal error: unexpectedly found nil while unwrapping an Optional value for the NSURLConnection.sendSynchronousRequest(...) call. I'm not sure what is going on. When I looked at the debug window, the only variable that is nil is response. This code looks very similar to the examples I've seen across the web.
If the request is finished within the time, no fatal error happens.
I want to be able to catch the timeout error. Anyone know what's wrong with the code?
You have the exclamation mark in the wrong place in this line,
var data:NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: &response, error: &error)!
It should be,
var data:NSData! = NSURLConnection.sendSynchronousRequest(request1, returningResponse: &response, error: &error)

Image request from URL fails

I have the following code:
func getImage(urlString: String, completionHandler: UIImage -> Void) -> NSURLSessionTask {
let request = NSMutableURLRequest(URL: NSURL(fileURLWithPath: urlString)!)
println("Executing image request: \(urlString)")
return session.dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("\tGET request failed: \(error!)")
} else {
println("\tGET request succeeded!")
let response = response as NSHTTPURLResponse
if response.statusCode == 200 {
let image = UIImage(data: data)
dispatch_async(dispatch_get_main_queue()) { // dispatch naar main thread (main_queue is thread# van main thread van app)
completionHandler(image!)
}
}
}
}
}
But when I execute it, I get the following output:
Executing image request: http://i.imgur.com/rfw7jrU.gif
GET request failed: Error Domain=NSURLErrorDomain Code=-1100
"The operation couldn’t be completed. (NSURLErrorDomain error -1100.)"
UserInfo=0x14e9b200 {NSErrorFailingURLKey=file:///http:/i.imgur.com/rfw7jrU.gif,
NSErrorFailingURLStringKey=file:///http:/i.imgur.com/rfw7jrU.gif,
NSUnderlyingError=0x14d8d600 "The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error -1100.)"}
I have no idea why this happens, as my nearly identical code that gets a .json file does work. I have tested this on both my device and the simulator.
This fails on all urls, not only imgur links. It also crashes before I put it into a UIImage, so that is also not the problem.
From the error message one can see that your URL is a file URL:
file:///http:/i.imgur.com/rfw7jrU.gif
and error -1100 is kCFURLErrorFileDoesNotExist (see CFNetwork Error Codes Reference).
You should replace
NSURL(fileURLWithPath: urlString)
by
NSURL(string: urlString)
// Swift 3+:
URL(string: urlString)

Resources