UIAlertView shows nothing

I'm trying to recieve data from a server to initialize my app. However sometimes due to the network problem I cannot recieve any data. So at this time, I need to show an alert and exit the app. Here's my code:

override func viewDidLoad() {
        super.viewDidLoad()
        /
        var params = Dictionary<String, String>();
        params["path"] = "/";
        let response = ApiRequest.get(ApiRequest.LIST_FOLDER, params: params);
        if (response?.objectForKey("succ") == nil) {
            self.presentViewController(UIAlertController.init(title: "Network Error", message: "No recieved data", preferredStyle: UIAlertControllerStyle.Alert), animated: true, completion: nil);
            abort();
        }
        if (response?.objectForKey("succ") as! String == "0") {
            UIAlertView(title: "Server Error", message: "Unable to process the request", delegate: nil, cancelButtonTitle: "Cancel").show();
            abort();
        }
        folderList = FolderList(folders: response?.objectForKey("msg") as! NSDictionary);
    }

But actually no alert view was shown when no network connection. Anybody knows why this happened?


PS: if I change "if (response?.objectForKey("succ") == nil)" to "if (response == nil)", it doesn't work. Even response is actually nil, it doesn't goes into the if block.

You abort your app, thus alerts are not shown.


You short code is full of should-not-dos and must-not-dos.


- Apps must not abort.

- You should not call synchronous method from inside viewDidLoad (or other methods executed in main thread).

(Assuming ApiRequest.get() is a synchronous method.)

- UIAlertController is introduced in iOS 8.0, and UIAlertView is deprecated in iOS 8.0, use UIAlertController for both alerts when run in iOS 8.0 or later, use UIAlert in iOS 7.x .


One more:

- This post should be in Cocoa Touch, not in Cocoa.

Thank you for that.

But actually, when I can't get a correct response from server, I cannot initialize my app at all. So I call a synchronous method and abort it when the response is nil or not correct.

I've also tried remove the abort code. But the AlertView still didnt show.

NSLog where you think it should occur to see if it actually gets that far/gets hit/...


Then update your code to remove any deprecated elements. Be sure to option-clean build folder as well.

Accepted Answer

But actually, when I can't get a correct response from server, I cannot initialize my app at all.

You should use asynchronous call even in such cases. Create a view for uninitialized state, and give user a chance to retry.


But the AlertView still didnt show.

How bad your usage of UIAlertController/UIAlertVIew is another thing, and can be fixed after other things got normal.


I believe you know that apps which abort would not be approved for App Store. And even if you were writing an in-house enterprise app, you should not take such bad behavior into your app.

Always Be Prepared to Stop

Never quit an iOS app programmatically. People tend to interpret this as a crash. If something prevents your app from functioning as intended, you need to tell users about the situation and explain what they can do about it.

(See the original article in the link above to find "two good ways to do this".

UIAlertView shows nothing
 
 
Q