1

Am connecting to the services and receiving the data from backend. The code is as follows:

 _ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in

        if response != nil{

           var responseREcvd = response as? NSHTTPURLResponse
            if responseREcvd?.statusCode == 404 {
                let  alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert)
                let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                alertControler.addAction(alertAction)
                dispatch_async(dispatch_get_main_queue(), {
                    targetVC.presentViewController(alertControler, animated: true, completion: nil)
                })
            }
        }

The alert is displayed but it freezes the UI. When I press the OK action button, no action is invoked.

2 Answers2

0

Try this extension

  extension UIAlertController {

    func show() {
        present(true, completion: nil)
    }

    func present(
        animated: Bool, completion: (() -> Void)?) {
        if let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
            presentFromController(rootVC, animated: animated, completion: completion)
        }
    }

    private func presentFromController(controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
        self.addAction(UIAlertAction.init(title: "Ok", style: .Default, handler: { (true) in
            self.dismissViewControllerAnimated(true, completion: nil)
        }))
        if let navVC = controller as? UINavigationController,
            let visibleVC = navVC.visibleViewController {
            presentFromController(visibleVC, animated: animated, completion: completion)
        } else
            if let tabVC = controller as? UITabBarController,
                let selectedVC = tabVC.selectedViewController {
                presentFromController(selectedVC, animated: animated, completion: completion)
            } else {
                controller.presentViewController(self, animated: animated, completion: completion);
        }
    }
}

USE:

let alertController = UIAlertController(title: "At \((arr_day.objectAtIndex(index.row - 1) as! String)) End Time Missing", message: "", preferredStyle: .Alert)
alertController.show()

Hope this will work for you

Jitendra Modi
  • 2,344
  • 12
  • 34
-1

try to use NSOperationQueue:

 _ = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in

    if response != nil{

       var responseREcvd = response as? NSHTTPURLResponse
        if responseREcvd?.statusCode == 404 {
            let  alertControler = UIAlertController(title: "404", message: "Server Down.", preferredStyle: .Alert)
            let alertAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
            alertControler.addAction(alertAction)
            dispatch_async(dispatch_get_main_queue(), {NSOperationQueue.mainQueue().addOperationWithBlock {
                targetVC.presentViewController(alertControler, animated: true, completion: nil)}
            })
        }
    }
bero
  • 13
  • 4