Max
Max

Reputation: 2299

alamofire gives exact response in simulator but gives error in device

I'm working on project first time with swift language. I have used alamofire for web service api. I have created a common func for it.

class func postWebService(methodname:String,param:NSDictionary,userName:String,password:String, CompletionHandler:(success:Bool,response:NSDictionary) -> ())
    {
        let mainlink :String = "mymainurl"
        var link = mainlink + methodname
        var url:NSURL = NSURL(string: link)!

        let plainString = "\(userName):\(password)" as NSString
        // example : let plainString = "textuser:testpwd" as NSString
        let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
        let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

        Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!]


        request(.GET, url, parameters: param as? [String : AnyObject]).responseJSON { (req, res, jsonresp, error) -> Void in
            if(error != nil) {
                NSLog("Error: \(req)  \(error)")
                var errDict:NSDictionary = ["message":"\(error?.localizedDescription)"]
                CompletionHandler(success: false,response: errDict)
            }
            else {

                var json: NSDictionary = jsonresp! as! NSDictionary
                NSLog("%@",json)
                CompletionHandler(success: true,response: json)

            }
        }
    }

It is working fine and gives me response when I run it in simulator. But if I load my project in device than It gives me a error

Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x17dc4580 {NSDebugDescription=Invalid value around character 0.})

If there is a problem in code than it should not work in simulator also. And it is nice and working code for web service api. Any help will be appreciated. Thanks in advance.

==== EDIT====

Mention : it is server url.

I figured out one more thing. If I run this code in ios 8 and above device it is working fine. And this problem is occurred in ios 7 device. I'm dealing with iPod with ios7. I have checked in ios8 devices like iPhone and iPad, it is working fine.

Upvotes: 1

Views: 986

Answers (2)

Max
Max

Reputation: 2299

I have researched for this and mainly I checked all information of Alamofire on github. In this page I have found solution and it is my main and big mistake is that Alamofire have not support of ios 7. It Requires ios 8+.

requirements from Alamofire

Above My code is right and good for web service api. There is no problem in URL or in Code. My problem is for ios 7.0 which is not supported so this problem occured.

So, It is my own mistake, not of Alamofire Framework or code. But is should support ios7 because we can't ignore ios version 7 for our projects. There are may be lots of devices having ios7. Thanks for all support.

Upvotes: 0

Glenn
Glenn

Reputation: 2806

May or not be the case with you but I once had that error when accessing a url that was not correct. In my case it was pointing to localhost, which was perfect for the simulator but complete useless on the device. Double check your URL, make sure the web service is online and can be reached from your device... Hope this helps, if not others will chime in ;)

Upvotes: 1

Related Questions