仅在iOS 13.3设备上Alamofire API请求超时

问题描述 投票:1回答: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)
    }
}
swift alamofire ios13.3
1个回答
-1
投票

您可以改用以下代码:

 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)

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.