如果响应 returns 状态代码不是 200,则停止我的 NSURLSession?在 SWIFT
Stop my NSURLSession if the response returns a status code other than 200? in SWIFT
我正在尝试使用 NSURLSession 加载图像。
如果响应 returns 状态码不是 200,我该如何处理 "stop" NSURLSession?我想显示一个网络问题弹出窗口。
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
loadData()
}
func loadData()
{
self.downloadFullImageFrom("https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
dispatch_async(dispatch_get_main_queue())
{
//DO SOMETHING WITH images IF DOWNLOAD IS OK
//HOW CAN I KNOW IF dataTaskWithURL FAILED?
}
}
}
func downloadFullImageFrom(url:String, completion: ((image: UIImage?) -> Void)) {
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
if let httpResponse = response as? NSHTTPURLResponse
{
if httpResponse.statusCode == 200
{
completion(image: UIImage(data: data!))
}
else { return }
}
}
task.resume()
}
}
将return替换为
completion(image: nil)
在您的方法定义中。并检查 image
里面是否为 nil
dispatch_async
尝试使用 guard 解包你的可选值并添加 where 子句来控制预期结果,以防失败只是 return nil:
func loadData() {
downloadFullImageFrom("https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
dispatch_async(dispatch_get_main_queue()) {
guard let image = image else {
print("error loading image")
return
}
//DO SOMETHING WITH images IF DOWNLOAD IS OK
print(image.size)
}
}
}
func downloadFullImageFrom(link: String, completion: ((image: UIImage?) -> Void)) {
guard let url = NSURL(string: link) else {
completion(image: nil)
return
}
NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response , error) in
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let data = data, image = UIImage(data: data) where error == nil else {
// display a network issue pop up
print("======= statusCode =======")
print((response as? NSHTTPURLResponse)?.statusCode)
completion(image: nil)
return
}
completion(image: image)
}.resume()
}
Xcode 8.3.1 • Swift 3.1
func loadData() {
downloadFullImageFrom(link: "https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
DispatchQueue.main.async() {
guard let image = image else {
print("error loading image")
return
}
//DO SOMETHING WITH images IF DOWNLOAD IS OK
print(image.size)
}
}
}
func downloadFullImageFrom(link: String, completion: @escaping ((UIImage?) -> Void)) {
guard let url = URL(string: link) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse,
httpURLResponse.statusCode == 200,
let data = data,
let image = UIImage(data: data),
error == nil
else {
// display a network issue pop up
print("======= statusCode =======")
print((response as? HTTPURLResponse)?.statusCode ?? "")
completion(nil)
return
}
completion(image)
}.resume()
}
我正在尝试使用 NSURLSession 加载图像。 如果响应 returns 状态码不是 200,我该如何处理 "stop" NSURLSession?我想显示一个网络问题弹出窗口。
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
loadData()
}
func loadData()
{
self.downloadFullImageFrom("https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
dispatch_async(dispatch_get_main_queue())
{
//DO SOMETHING WITH images IF DOWNLOAD IS OK
//HOW CAN I KNOW IF dataTaskWithURL FAILED?
}
}
}
func downloadFullImageFrom(url:String, completion: ((image: UIImage?) -> Void)) {
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
if let httpResponse = response as? NSHTTPURLResponse
{
if httpResponse.statusCode == 200
{
completion(image: UIImage(data: data!))
}
else { return }
}
}
task.resume()
}
}
将return替换为
completion(image: nil)
在您的方法定义中。并检查 image
里面是否为 nil
dispatch_async
尝试使用 guard 解包你的可选值并添加 where 子句来控制预期结果,以防失败只是 return nil:
func loadData() {
downloadFullImageFrom("https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
dispatch_async(dispatch_get_main_queue()) {
guard let image = image else {
print("error loading image")
return
}
//DO SOMETHING WITH images IF DOWNLOAD IS OK
print(image.size)
}
}
}
func downloadFullImageFrom(link: String, completion: ((image: UIImage?) -> Void)) {
guard let url = NSURL(string: link) else {
completion(image: nil)
return
}
NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response , error) in
guard
let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
let data = data, image = UIImage(data: data) where error == nil else {
// display a network issue pop up
print("======= statusCode =======")
print((response as? NSHTTPURLResponse)?.statusCode)
completion(image: nil)
return
}
completion(image: image)
}.resume()
}
Xcode 8.3.1 • Swift 3.1
func loadData() {
downloadFullImageFrom(link: "https://www.google.fr/logos/doodles/2015/holidays-2015-day-3-6399865393250304-5649050225344512-ror.jpg") { image in
DispatchQueue.main.async() {
guard let image = image else {
print("error loading image")
return
}
//DO SOMETHING WITH images IF DOWNLOAD IS OK
print(image.size)
}
}
}
func downloadFullImageFrom(link: String, completion: @escaping ((UIImage?) -> Void)) {
guard let url = URL(string: link) else {
completion(nil)
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse,
httpURLResponse.statusCode == 200,
let data = data,
let image = UIImage(data: data),
error == nil
else {
// display a network issue pop up
print("======= statusCode =======")
print((response as? HTTPURLResponse)?.statusCode ?? "")
completion(nil)
return
}
completion(image)
}.resume()
}