Swift Alamofire quit when request timed out - ios

I wonder how I can exit out of my function when request timed out.
If I put my server offline and try to do some calls with this function the app will crash due to:
fatal error: unexpectedly found nil while unwrapping an Optional value
My Alamofire v3.x function looks like this:
static func loginWithFb(facebookId: String, email: String, username: String, response: (token: String?, errorVal: JSON?) -> ()) {
let urlString = baseURL + ResourcePath.FbLogin.description
let parameters: [String: AnyObject] = [
"facebook_id": facebookId,
"user_type": 1,
"email": email,
"username": username
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON{ (responseData) -> Void in
let json = JSON(responseData.result.value!)
let token = json["api_key"].string
response(token: token, errorVal: json)
}
}
I get nil from:
let json = JSON(responseData.result.value!)
and the error:
FAILURE: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x7fc39349c4a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://mysite.dev/fb, NSErrorFailingURLKey=http://mysite.dev/fb,
_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
fatal error: unexpectedly found nil while unwrapping an Optional value
So how can I exit the call if the request timed out? preventing it from trying to get the json data
Thanks,

You can increase the timeout interval if your service getting long time.
let request = NSMutableURLRequest(URL: NSURL.init(string: "your url")!)
request.HTTPMethod = "GET"
request.timeoutInterval = 250 // Set your timeout interval here.
Alamofire.request(request).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) { //Check for response
print(responseData.result.value!)
}
}
And must check that the response is not nil to resolve the issue of fatal error.

you need to know more about swift variable unwrapping, just a simple if(myJsonData != nil) would prevent the app crash, you can also add an alert box in case that there is no data on server or the server is down etc...
static func loginWithFb(facebookId: String, email: String, username: String, response: (token: String?, errorVal: JSON?) -> ()) {
let urlString = baseURL + ResourcePath.FbLogin.description
let parameters: [String: AnyObject] = [
"facebook_id": facebookId,
"user_type": 1,
"email": email,
"username": username
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON{ (responseData) -> Void in
let myJsonData = JSON(responseData.result.value!)
if(myJsonData != nil)
{
let token = myJsonData["api_key"].string
}
else
{
// show alert box
}
}
}

Your issue is here
let json = JSON(responseData.result.value!)
you are force unwrapping value! instead you should wrap it in an if let
if let json = JSON(responseData.result.value) as? NSDictionary {
let token = json["api_key"].string
response(token: token, errorVal: json)
}
The timeout is fine and is not effecting your app in any way.

Related

Swift 3 - issue to create easy request method POST (URLRequestVonvertible)

I am developing an application with Swift 3.0 and IOS 10 in Xcode 8.3.2. But I have a problem when I try to retrieve the JSON from this APIRest (http://schematic-ipsum.herokuapp.com/). What is the problem? Or how you would make the call. If you need more information regarding the code, tell me, but basically, I just want to make a call to that page to recover the biography.
enter image description here
My code is this:
import AlamofireDomain
import Alamofire
import ObjectMapper
class AuthorDAO : SimpleDAO {
func getBiography(_ parameters: Dictionary<String, Int>,
callbackFuncionOK: #escaping
(PropertiesAuthorModel)->(),
callbackFunctionERROR: #escaping (Int,NSError)->()) {
let ulr = NSURL( string: "http://schematic-ipsum.herokuapp.com/" as String)
let request = NSMutableURLRequest(url: ulr! as URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Constent-Type")
let data = try! JSONSerialization.data(withJSONObject: parameters, options: JSONSerialization.WritingOptions.prettyPrinted)
let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if let json = json {
print(json)
}
request.httpBody = json!.data(using: String.Encoding.utf8.rawValue)
Alamofire.request(request as URLRequest)
.responseJSON { response in
if response.result.isSuccess{
if let status = response.response?.statusCode {
switch(status){
//MARK: CONTROL ON DIFERENTS KINDS OF RESPONSES ON SERVER
case 200:
if let value = response.result.value {
let biographyResponse = Mapper<PropertiesAuthorModel>().map(JSONObject: value)
callbackFuncionOK(biographyResponse!)
}
//case 400: {} ..., case 404: {} ...
default:
break
}
}
}
//MARK: ERROR ON SERVER
else {
var statusCode = -1
if let _response = response.response {
statusCode = _response.statusCode
}
var nsError: NSError = NSError(domain: Constants.UNKNOWN_HTTP_ERROR_MSG,
code: Constants.UNKNOWN_HTTP_ERROR_ID,
userInfo: nil)
if let _error = response.result.error {
nsError = _error as NSError
}
callbackFunctionERROR(statusCode,nsError)
}
}
}
And the error is: "Error Domain=Alamofire.AFError Code=4 "JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format." 400"
The problem is that it does not enter into the "if response.result.isSuccess {" because the result of the response is "nil" and goes directly to the "else". Showing the error that I have commented. Can it be because I have to send the JSON format in the httpBody request? How can I do it?
Your question should be more clear. "What is the problem?" isn't possible to answer without more information.
To issue HTTP request, take a look at Alamofire, an HTTP networking library written in Swift: https://github.com/Alamofire/Alamofire
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}

Alomofire POST request in swift

I am using the following code in my swift 2.0 project. I cannot add Alamofire.request though I added "import Alamofire". I have to create object of Alamofire and then access through it.
This is how I create the object :
let manager = Alamofire.Manager.sharedInstance
manager.request(NSURLRequest(URL: NSURL(string: "http://httpbin.org/get")!))
let parameters = ["foo": "bar"]
manager.request(.POST, "url", parameters: parameters)
.responseJSON { request, response, json, error in
print("request: \(request)")
}
I am new to both Alamofire and swift. Can anybody tell how can I get response from the above code in a completion handler and why I cannot use Alamofire.request instead of manager.request.
Please see my Post method and Hope It helps
Post Method:
/**
** POST Method for calling API
* Services gateway
* Method get response from server
* #parameter -> requestObject: request josn object ,apiName: api endpoint
* #returm -> void
* #compilationHandler -> success: status of api, response: respose from server, error: error handling
**/
static func getDataWithObject( requestObject: NSDictionary, apiName : NSString,
completionHandler:
(success : Bool, response : NSDictionary, error : ErrorType?) -> Void) {
// Make Url
let url = NSURL(string: apiName as String)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
//request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// Call the method to request and wait for the response
// #param ->
// #return ->
Alamofire.request(.POST, url!, parameters:requestObject as? [String : AnyObject], encoding: .JSON)
.responseJSON {responseRequest, responseResponse, responseResult in
// Switch for Success or Error
switch responseResult {
// If the API return succesfull response
case .Success(let data):
let data_ar = data as! NSDictionary
print(data_ar)
// Get the Status if 0 then error if 1 then succes
// From our server side
if let str = data_ar.valueForKey("OK") as? Bool {
// Check if the status is OK and no error from
// our server side
if ( str ) {
print("Response from Server %#", data_ar)
// Cast the response and pss to handler
// To notify
completionHandler(success: true, response:data_ar
, error:responseResult.error )
} else {
print("Error from Our Server %#", data_ar)
let str = data_ar.valueForKey("message") as! NSString
self.showAlertView(str, title: "Error From Server")
}
}
case .Failure(let data, let error):
print("Request failed with error: \(error)")
print(data)
print((error as! NSError).localizedDescription)
self.showAlertView((error as! NSError).localizedDescription, title: "Error From Server")
}
}
}
Request not always in JSON please check your request :
Following are examples to use Alamofire with the Swift 2 :
GET - JSON
Alamofire.request(.GET, "http://api.androidhive.info/contacts/", parameters: nil, encoding: .JSON, headers: nil).responseJSON { (req, res, json) -> Void in
print("\(res?.allHeaderFields)")
print("\(json.value!)")
}
POST - without JSON
Alamofire.request(.POST, "http://httpbin.org/get", parameters: ["foo": "bar"], encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
print(res)
print(data)
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(dataString)
}

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.

Swift - Expected expression in list of expressions

I'm new to Swift been reading but have no clue what this means. On the line of code below, I have "Expected expression in list of expressions" after parameters[String]. AS well at the same point it is looking for "Expected ',' separator. I believe these are related.
AppDelegate.submitLacunaRequest(module: "empire", method: "login", parameters[String]:["myuserid", "mypassword", "mykey"]) {
responseObject, error in
// some network error or programming error
if error != nil {
println("error = \(error)")
println("responseObject = \(responseObject)")
return
}
// network request ok, now see if login was successful
if let responseDictionary = responseObject as? NSDictionary {
if let errorDictionary = responseDictionary["error"] as? NSDictionary {
println("error logging in (bad userid/password?): \(errorDictionary)")
} else if let resultDictionary = responseDictionary["result"] as? NSDictionary {
println("successfully logged in, refer to resultDictionary for details: \(resultDictionary)")
} else {
println("we should never get here")
println("responseObject = \(responseObject)")
}
}
}
Here is the related code from AppDelegate
public func submitLacunaRequest (#module: String, method: String, parameters: AnyObject, completion: (responseObject: AnyObject!, error: NSError!) -> (Void)) -> NSURLSessionTask? {
let session = NSURLSession.sharedSession()
let url = NSURL(string: "https://us1.lacunaexpanse.com")?.URLByAppendingPathComponent(module)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/json-rpc", forHTTPHeaderField: "Content-Type")
let requestDictionary = [
"jsonrpc" : "2.0",
"id" : 1,
"method" : "login",
"params" : ["myuserid", "mypassword", "mykey"]
]
var error: NSError?
let requestBody = NSJSONSerialization.dataWithJSONObject(requestDictionary, options: nil, error: &error)
if requestBody == nil {
completion(responseObject: nil, error: error)
return nil
}
request.HTTPBody = requestBody
let task = session.dataTaskWithRequest(request) {
data, response, error in
// handle fundamental network errors (e.g. no connectivity)
if error != nil {
completion(responseObject: data, error: error)
return
}
// parse the JSON response
var parseError: NSError?
let responseObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parseError) as? NSDictionary
if responseObject == nil {
// because it's not JSON, let's convert it to a string when we report completion (likely HTML or text)
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as String
completion(responseObject: responseString, error: parseError)
return
}
completion(responseObject: responseObject, error: nil)
}
task.resume()
return task
}
You are using external parameter name for a parameter when calling the function, but the external parameter is not defined in your function declaration. Simply use it this way.
submitLacunaRequest(module: "empire", "login", ["myuserid", "mypassword", "mykey"]) {
You're calling the function incorrectly. You don't need the [String] in the parameters param...
AppDelegate.submitLacunaRequest(module: "empire", method: "login", parameters: ["myuserid", "mypassword", "mykey"]) {
...
}
I called my function parameter protocol.
If I was to try using this property as usual, I would notice it to be written in pink being a keyword and I would rename it.
Instead, I used this property in a string like this and I didn't get any clues from the compiler:
func configure(_ protocol: Protocol, host:String, port:String) {
urlString = "\(protocol)://\(host):\(port)"
}
I spend good 5 minutes confused out of my mind by this error, but then I figured it out. The problem was in the name of the parameter.
I didn't want to rename the parameter, so I ended up writing it like this:
urlString = "\(`protocol`)://\(host):\(port)"

Resources