vendredi 29 mars 2019

ActivityIndicator (spinner) getting error Unexpectedly found nil while unwrapping an Optional value

In my shared class i write spinner code, it's working fine but, some times it's getting error.

Error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

My code in shared class:

import UIKit

class SharedClass: NSObject {

static let sharedInstance = SharedClass()

var transparentView:UIView!
var spinner = UIActivityIndicatorView()


//Show activity indicator
func activityIndicator(view:UIView) {

    DispatchQueue.main.async {
        let window = UIApplication.shared.keyWindow! //Error : Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
        self.transparentView = UIView()
        self.transparentView.frame = CGRect(x: 0, y: 0, width: window.frame.width, height: window.frame.height)
        self.transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        window.addSubview(self.transparentView)

        if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
            self.spinner = UIActivityIndicatorView(style: .whiteLarge)
            self.spinner.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        } else {
            self.spinner = UIActivityIndicatorView(style: .white)
            self.spinner.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        }
        self.spinner.center = view.center
        self.transparentView.addSubview(self.spinner)
        self.spinner.startAnimating()

        DispatchQueue.main.asyncAfter(deadline: .now() + 600.0) {//Stop spinner after 10 Sec's
            self.stopActivityIndicator()
            //self.transparentView.removeFromSuperview()
        }
    }
}

//Stop activity indicator
func stopActivityIndicator() {
    DispatchQueue.main.async {
        self.spinner.stopAnimating()
        self.spinner.removeFromSuperview()
        self.transparentView.removeFromSuperview()
    }
}

 private override init() {

}

}

I'm calling like this

SharedClass.sharedInstance.activityIndicator(view: self.view)//Play spinner

SharedClass.sharedInstance.stopActivityIndicator() //Stop spinner

This is my login Btn code

@IBAction func onClickLoginBtn(_ sender: UIButton) {

    let networkReachability = Reachability.forInternetConnection()
    let networkStatus:Int = (networkReachability?.currentReachabilityStatus())!.rawValue
    print(networkStatus)
    if networkStatus == NotReachable.rawValue {
        let msg = SharedClass.sharedInstance.noNetMsg
        SharedClass.sharedInstance.alertWindow(title: "", message: msg)
    } else {

        SharedClass.sharedInstance.activityIndicator(view: self.view)//Play spinner
        let parameters = "imei=\(deviceID)&devid=\(deviceID)&ver=\(ver)"
        print("Parameters : \(parameters)")
        var request = URLRequest(url: URL(string: url)!)

        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"

        print("URL : \(request)")

        request.httpBody = parameters.data(using: .utf8)

        let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error
            SharedClass.sharedInstance.stopActivityIndicator() //Stop spinner
            print("error=\(String(describing: error))")
            SharedClass.sharedInstance.alertWindow(title: "", message: "\(String(describing: error!.localizedDescription))")
            return
            }

            SharedClass.sharedInstance.stopActivityIndicator() //Stop spinner

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 500 {
                SharedClass.sharedInstance.alertWindow(title: "", message: "Server Error")
            } else if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(String(describing: response))")
            }

            do {
                let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject]
                print(response!)


                if status == "SUCCESS" {


                } else {
                    let message = res[0]["message"] as! String
                    //Call alert function
                    SharedClass.sharedInstance.alertWindow(title: "", message: message)
                }

            } catch let error as NSError {
                print(error)
                print(error.localizedDescription)
            }
        }

        task.resume()

    }

}

Here when i click login btn first time it's working fine. But when i get response Request timed out.

Error

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x7f9eab767d60 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}

Alert will be displayed. In this scenario when i click login btn second time now app crashed with error : Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value .

1) Is my code formate is correct or not(means function calling and JSON post code)

2) How to solve this error.

Aucun commentaire:

Enregistrer un commentaire