好吧,所以我找到了一些非常相似的问题的答案,但大多数人建议使用if语句或try/catch块,而我的代码都具有两者。我正在打电话给我的Web服务,该服务返回JSON,我使用JsonSerialization将其拉出以解析。由于标题暗示我的应用程序在没有互联网时会崩溃和燃烧,我希望有人能告诉我最好的解决这个问题的方法。我将在下面放置我的方法:
func getCategories() {
activityIndicator?.startAnimating()
tableView.isUserInteractionEnabled = false
categoryArray = []
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
let getQuizTitlesURL = URL(string: "https://myservice.com/my/directory/selectcategories.php")
URLSession.shared.dataTask(with: getQuizTitlesURL! as URL, completionHandler: {(data, response, error) in
do{
if let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSArray {
var name:String
var categoryId:Int
var quizCount:Int
for index in 0...parsedData.count-1 {
let aObject = parsedData[index] as! [String : AnyObject]
name = (aObject["Name"] as? String)!
quizCount = Int(aObject["Count"] as! String)!
categoryId = (Int((aObject["ID"] as? String)!)!)
let category:Category = Category(name: name, quizCount: quizCount, categoryId: categoryId)
self.categoryArray.append(category)
}
}
if let HTTPResponse = response as? HTTPURLResponse {
print(HTTPResponse)
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
print("Success")
}
}
}catch let error as NSError {
print(error)
}
DispatchQueue.main.async {
self.tableView.reloadData()
self.activityIndicator?.stopAnimating()
self.tableView.isUserInteractionEnabled = true
}
}).resume()
}
不要使用武力解开。
guard let data = data else {
// no data
return
}
do {
if let parsedData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSArray {
. . .
,正如@Joaofs所说,请在请求之前检查网络可用性。即使您检查此问题,请求响应期间的一切都可能。就像在请求之后刚断开网络一样。
我个人使用可及性,我在拨打电话之前检查连接是否可用
var reachability = Reachability()! // I declare this in the appDelegate as global variable
func getCategories() {
if reachability.isReachable {
// your code
} else {
let alertViewController = UIAlertController(title: "No Connection" , message: "There is something wrong with your internet Connection. Please check and try again", preferredStyle: .alert)
let okAction = UIAlertAction(title: okTitle, style: .default) { (uialertAction) in
alertViewController.dismiss(animated: true, completion: nil)
}
alertViewController.addAction(okAction)
self.present(alertViewController, animated: true, completion: nil)
}
}
如果没有网络,我不会尝试进行API调用。
另一方面,您应该确保在尝试序列化有效载荷之前获得HTTP状态代码,指示成功的响应。
制作一个新的Swift文件(可可类(,然后添加名称ressability.swift。在此文件中添加此代码:
import UIKit
import SystemConfiguration
protocol Utilities {
}
extension NSObject:Utilities{
enum ReachabilityStatus {
case notReachable
case reachableViaWWAN
case reachableViaWiFi
}
var currentReachabilityStatus: ReachabilityStatus {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil,$0)
}
}) else {
return .notReachable
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return .notReachable
}
if flags.contains(.reachable) == false {
// The target host is not reachable.
return .notReachable
}
else if flags.contains(.isWWAN) == true {
// WWAN connections are OK if the calling application is using the CFNetwork APIs.
return .reachableViaWWAN
}
else if flags.contains(.connectionRequired) == false {
// If the target host is reachable and no connection is required then we'll assume that you're on Wi-Fi...
return .reachableViaWiFi
}
else if (flags.contains(.connectionOnDemand) == true || flags.contains(.connectionOnTraffic) == true) && flags.contains(.interventionRequired) == false {
// The connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs and no [user] intervention is needed
return .reachableViaWiFi
}
else {
return .notReachable
}
}
}
现在要实现您要实施网络服务和这样的东西的地方。
func checkReachability(){
if (currentReachabilityStatus == .reachableViaWiFi) || (currentReachabilityStatus == .reachableViaWWAN) {
// if wifi-connection or mobile-network-connection
// YOUR_CODE
} else {
print("There is no internet connection")
// make an alert to go to settings and enable mobile data/wifi.
let alertController = UIAlertController (title: "Connectivity error!", message: "Go to Settings -> Enable Wi-Fi/Mobile Data", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: "App-Prefs:root") else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: (success)") // Prints true
})
}
}
alertController.addAction(settingsAction)
present(alertController, animated: true, completion: nil)
}
}
好吧,不要感谢我。祝你好运!!!!:D