Swift: the ability to make an HTTP request over Wi-Fi, but not over the Cellullar network

I followed the quick tutorial http://jamesonquave.com/blog/making-a-post-request-in-swift/

here is the code i use to make an http call:

func post(params : Dictionary<String, String>, url : String, postCompleted : (succeeded: Bool, msg: String) -> ()) {
    var request = NSMutableURLRequest(URL: NSURL(string: url))
    var session = NSURLSession.sharedSession()
    session.configuration.allowsCellularAccess = true
    request.HTTPMethod = "POST"

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
    if (err != nil)
    {println("An error occurred during conversion to JSON")}
    println(request.HTTPBody)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    //request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: {(data:NSData!, response:NSURLResponse!, error: NSError!) -> Void in
        println("Response: \(response)")
        var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("Body: \(strData)")
        var msg = "No message"

        println(response)
        var respHTTP = response as? NSHTTPURLResponse
        if (respHTTP?.statusCode == 200)
        {
            println("Command executed.")
            postCompleted(succeeded: true, msg: "Command Executed.")
        }
        else
        {
            println("Command not executed.")
            postCompleted(succeeded: false, msg: "Command NOT Executed. \(respHTTP?.statusCode)")
        }
    })

    task.resume()
}

It works great with a Wi-Fi connection, but for some reason does not work with a WWAN connection. The request does not seem to be coming out. Please share tips where this went wrong, or do I need to configure something at the system level?

I use Xcode 6 and iOS 8 on my iPhone 6, but tested on iPhone 5 with iOS 7.1, got the same result.

Addition: NSError error! domain: "NSURLErrorDomain" - code: 18446744073709550615 0x00000001700571f0 The Handler termination is called, and I attached an error message. The answer is nil

: Error Domain = NSURLErrorDomain Code = -1001 " . (NSURLErrorDomain error -1001.)" UserInfo = 0x17027d680 {NSErrorFailingURLStringKey = xx.xx.xx.xx: 8080/carservices/CloseCar, NSUnderlyingError = 0x174059890 " cannt (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey = xx.xx.xx.xx: 8080/carservices/CloseCar}

, - -, NSURLErrorTimedOut = -1001 , Wi-Fi, WWAN...

+4

All Articles