Jimmy Pun
Jimmy Pun

Reputation: 91

Alamofire 4 FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled"

Hi I am try to use Alamofire for my project but the error come out. Here is my requesting code //Google testing

  Alamofire.request("http://google.com").responseString{
        response in
        debugPrint(response)
    }.session.invalidateAndCancel()

Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-999 "cancelled"

UserInfo={NSErrorFailingURLKey=http://google.com/,

NSLocalizedDescription=cancelled,

NSErrorFailingURLStringKey=http://google.com/}

//Own server testing

    Alamofire.request("https://10.68.24.127:4533").responseString{
        response in
        debugPrint(response)
    }.session.invalidateAndCancel()

same result

    class NetworkManager {

var manager: SessionManager?

init() {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "https://10.68.24.127:4533" : .disableEvaluation
    ]
    let configuration = URLSessionConfiguration.default
    manager = Alamofire.SessionManager(
        configuration: configuration,
        serverTrustPolicyManager :ServerTrustPolicyManager(policies: serverTrustPolicies)
    )
}
    }

I set the NSAllowsArbitraryLoads to true and NSExceptionDomains.

Where is the problem?

Upvotes: 1

Views: 2892

Answers (3)

Mohammad Reza Koohkan
Mohammad Reza Koohkan

Reputation: 1734

There could be a lot of reason to why your requests "cancelled".
If you are facing that a request cancels immediately you can refer to this issue in Alamofire repository issues

jshier commented on Oct 10, 2016

An unexpected error -999 almost always means your SessionManager was deallocated, cancelling any ongoing requests. I suggest you create a singleton value for your custom SessionManager, or perhaps just reevaluate if you really need one.

if you create a singleton value for your object it remains in memory and prevent from deallocate

and another thing that i avoid is to name your variables diffrent, a sessionManager is in Alamofire and your variable is also called sessionManager.

Alamofire 4.7 , Swift 4

import Alamofire

class Networking {

  public static let sharedManager: SessionManager = {
      let configuration = URLSessionConfiguration.default
      configuration.timeoutIntervalForRequest=20
      let manager = Alamofire.SessionManager(configuration: configuration, delegate: SessionManager.default.delegate)
      return manager
  }()
}

Alamofire 5.4.4 , Siwft 5.2

import Alamofire

class Networking {

  static let APIManager: Session = {
      let configuration = URLSessionConfiguration.default
      configuration.timeoutIntervalForRequest = 20
      let delegate = Session.default.delegate
      let manager = Session.init(configuration: configuration,
                                 delegate: delegate,
                                 startRequestsImmediately: true,
                                 cachedResponseHandler: nil)
      return manager
  }()
}

Upvotes: 0

Debaprio B
Debaprio B

Reputation: 531

Most likely you should be checking if there is an implementation of authentication challenge delegate method and check if its calling NSURLSessionAuthChallengeCancelAuthenticationChallenge.

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
                                             completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler

Upvotes: 0

Subho
Subho

Reputation: 1

Check this documentation from Alamofire app-transport-security

try to add the following in your .plist file

<key>NSAppTransportSecurity</key><dict>
<key>NSExceptionDomains</key>
<dict>
    <key>url.com</key>
    <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <key>NSExceptionRequiresForwardSecrecy</key>
        <false/>
        <key>NSIncludesSubdomains</key>
        <true/>
    </dict>
</dict>

Upvotes: 0

Related Questions