Alamofire: finished with error - code: -1001

后端 未结 3 2218
走了就别回头了
走了就别回头了 2020-12-04 03:01

Code like this:

import Foundation
import Alamofire
struct Request {
var alamoFireManager : SessionManager?
init() {
    let configuration = URLSessionConfi         


        
相关标签:
3条回答
  • 2020-12-04 03:05

    SessionManager not works for me correctly. It's better to using code like this in Alamofire:

    let request = URLRequest(url: url)
    request.httpMethod = "GET"
    //Some configuration
    
    
    Alamofire.request(request).responseJSON {
                    response in
    
    }
    
    0 讨论(0)
  • 2020-12-04 03:07

    I have done this and it's working (swift 4 Code).

    import UIKit
    import Alamofire
    
    class SplashViewController: UIViewController {
    
        var alamoFireManager = Alamofire.SessionManager.default
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.callPostApi()
    
        }
    
        func callPostApi() {
    
            let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = 20 // seconds
            configuration.timeoutIntervalForResource = 20
            configuration.requestCachePolicy = .useProtocolCachePolicy
            alamoFireManager = Alamofire.SessionManager(configuration: configuration)
    
            alamoFireManager.request(url, method: .post, parameters: ["jsondata":base64EncodedString])
                .responseJSON { response in
    
                    switch (response.result) {
                    case .success:
    
                        print(response.data as? Data)
    
                        if let json = response.result.value {
                            print("JSON: \(json)") // Here is your JSON Response
                        }
                        //do json stuff
    
                    case .failure(let error):
    
                        if error._code == NSURLErrorTimedOut || error._code == -1005{
                            //HANDLE TIMEOUT HERE
                            print("TimeOut")
                        }
                        print("\n\nAuth request failed with error:\n \(error)")
                        break
                   }
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-04 03:17

    For me was a timeout error. One option is increase the value of the timeout (bad option), the better is improve the performance of your API/Application.

    0 讨论(0)
提交回复
热议问题