简体   繁体   中英

Best way to keep JSONSerialization from crashing my app when there is no internet?

Okay so I found a few different answers to very similar questions but most suggest using an if statement or a try/catch block and my code has both. I'm making a call to my web service which returns JSON, which I use JSONSerialization to pull it out for parsing. As the title suggests my app crashes and burns when there is no internet and I was hoping someone could tell me the best way to handle this issue. I'll put my method below:

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()
}

I personally use Reachability and I check if the connection is available before making the call

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)
  }
}

Do not use force unwrapping ever.

guard let data = data else { 
    // no data
    return
}
do {
    if let parsedData = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSArray {
. . .

Also, as @joaofs said, check network availability before you request. Even you check this, everything is possible during request-response. Like network disconnected just after request.

I wouldn't try to make an API call if there is no network available check this library .

On the other hand, you should make sure you get an HTTP status code indicating a successful response before you try to serialize the payload.

Make a new swift file (Cocoa class) and add name it Reachability.swift. In this file add this code:

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
    }
}

}

Now implement this where you want to implement your web service and stuff like this.

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)

    }
}

Ok don't thank me. Good luck!!!! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM