超时请求后如何返回上一屏幕?
How to go back to previous screen after a timeout request?
我正在尝试将网络会话创建者设置为在超时请求用完时返回到上一个屏幕。截至目前,我还不完全确定在哪里或如何执行它。这是代码:
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONData = ((Data) -> Void)
func getJSONData(type: String, urlExtension: String, completion: @escaping JSONData) {
configuration.timeoutIntervalForRequest = 5
configuration.timeoutIntervalForResource = 5
let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
completion(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
print("Error: \(error?.localizedDescription)")
}
})
dataTask.resume()
}
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONData = ((Data) -> Void)
func getJSONData(type: String, urlExtension: String, onSucceed : @escaping JSONData , onFailure: @escaping (_ error:NSError)-> Void ) {
configuration.timeoutIntervalForRequest = 5
configuration.timeoutIntervalForResource = 5
let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
onSucceed(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
onFailure(error! as NSError)
}
})
dataTask.resume()
}
在您调用此方法的视图控制器中,我会像这样使用它。
NetworkManager.getJSONData(type: "", urlExtension: "",onSucceed {
//doSmth
}, on Failure{(error) in
//show Error
// if it is pushed
_ = self.navigationController?.popViewController(animated: true)
// or if its presented
// self.navigationController?.dismiss(animated: true, completion: nil)
}
你可以试试这个。这对我有用
_ = self.navigationController?.popViewController(animated: true)
这将从 UINavigationController
堆栈中弹出最顶部的 UIViewController
,因此您之前的屏幕将可见。
如果您使用 show segue 进入当前 VC,您可以使用:
_ = self.navigationController?.popViewController(animated: true)
如果您使用模式转场,您将使用:
_ = self.navigationController?.dismiss(animated: true)
我正在尝试将网络会话创建者设置为在超时请求用完时返回到上一个屏幕。截至目前,我还不完全确定在哪里或如何执行它。这是代码:
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONData = ((Data) -> Void)
func getJSONData(type: String, urlExtension: String, completion: @escaping JSONData) {
configuration.timeoutIntervalForRequest = 5
configuration.timeoutIntervalForResource = 5
let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
completion(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
print("Error: \(error?.localizedDescription)")
}
})
dataTask.resume()
}
lazy var configuration: URLSessionConfiguration = URLSessionConfiguration.default
lazy var session: URLSession = URLSession(configuration: self.configuration)
typealias JSONData = ((Data) -> Void)
func getJSONData(type: String, urlExtension: String, onSucceed : @escaping JSONData , onFailure: @escaping (_ error:NSError)-> Void ) {
configuration.timeoutIntervalForRequest = 5
configuration.timeoutIntervalForResource = 5
let request = URLRequest(url: URL(string:"\(baseURL)\(type)/\(urlExtension)?api_key=\(apiKey)")! )
let dataTask = session.dataTask(with: request, completionHandler: { (data, response, error) in
if error == nil {
if let httpResponse = response as? HTTPURLResponse {
switch (httpResponse.statusCode) {
case 200:
if let data = data {
onSucceed(data)
}
default:
print(httpResponse.statusCode)
}
}
} else {
onFailure(error! as NSError)
}
})
dataTask.resume()
}
在您调用此方法的视图控制器中,我会像这样使用它。
NetworkManager.getJSONData(type: "", urlExtension: "",onSucceed {
//doSmth
}, on Failure{(error) in
//show Error
// if it is pushed
_ = self.navigationController?.popViewController(animated: true)
// or if its presented
// self.navigationController?.dismiss(animated: true, completion: nil)
}
你可以试试这个。这对我有用
_ = self.navigationController?.popViewController(animated: true)
这将从 UINavigationController
堆栈中弹出最顶部的 UIViewController
,因此您之前的屏幕将可见。
如果您使用 show segue 进入当前 VC,您可以使用:
_ = self.navigationController?.popViewController(animated: true)
如果您使用模式转场,您将使用:
_ = self.navigationController?.dismiss(animated: true)