1

仅在 iOS 13.3 上获取请求超时,但在 iOS 13.3 以下可以正常工作。

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." 
UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x60000193b060 
{Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, 
_kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <8F950888-C8E6-47B6-9FD8-78719B4FD231>.<8>,
 _NSURLErrorRelatedURLSessionTaskErrorKey=(
        "LocalDataTask <8F950888-C8E6-47B6-9FD8-78719B4FD231>.<8>"
    ), 
NSLocalizedDescription=The request timed out., 
NSErrorFailingURLStringKey=https://api.url.com/oauth, 
NSErrorFailingURLKey=https://api.url.com/oauth, _kCFStreamErrorDomainKey=4}

请帮助我。

代码片段是 -

func commonNetworkCallWithHeader(header :[String:String],url:String,method:HTTPMethod,parameters : [String:Any]?,completionHandler:@escaping (JSON?,String?)->Void) {
    let configuration = URLSessionConfiguration.background(withIdentifier: "")
    let manager = Alamofire.SessionManager(configuration: configuration)
    manager.session.configuration.timeoutIntervalForRequest = 60
    manager.startRequestsImmediately = true
    manager.request(url, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
        if(response.result.isSuccess){
            if let data = response.result.value{
                let json = JSON(data)
                completionHandler(json,nil)
                return
            }
        }
        completionHandler(nil,response.result.error?.localizedDescription)
    }
}
4

1 回答 1

-2

您可以改用以下代码:

 func serverRequest(url : String, params : [String : Any], method : HTTPMethod, completionHandler: @escaping ((_ responseJson : [String : Any])->Void)) {

    if Reachability.isConnectedToNetwork() {
        Alamofire.request(url, method: method, parameters:params,encoding: JSONEncoding.default, headers: nil).responseJSON {     (responseData) -> Void in

            if let json = responseData.result.value {

                if let jsonData = json as? [String: Any]
                {
                        completionHandler(jsonData)

                }
                else
                {
                                          let errorDict : [String : Any] = ["message": "Something went wrong, Please try again", "statusCode": 500, "status": 0]
                                          completionHandler(errorDict)

                }
            }

            else
            {
                                    let errorDict : [String : Any] = ["message": "Something went wrong, Please try again", "statusCode": 500, "success": false]
                                    completionHandler(errorDict)

            }
        }
    }
    else {
                    let errorDict : [String : Any] = ["message": "No Network Found", "statusCode": 104, "status": 0]
                  completionHandler(errorDict)

    }
}
于 2020-01-02T06:27:05.420 回答