thebootsie123 thebootsie123 - 1 year ago 71
Objective-C Question

How do I create a UIAlertController in a webview?

I've been searching on the forums and still don't really know how to use an UIAlertController in a webview. I found a couple of questions although I didn't really get how to fix it. I'm a beginner if you can't already tell. What I'm trying to do is, if the user's internet turns off a message shows up. Although I get an error. Here's the error I get.

No visible @interface for 'WebViewController' declares the selector 'presentedViewController:animated:completion:'

And here's my code in the WebViewController's .m file.

- (void) webView:(UIWebView*) webView didFailLoadWithError:(nonnull NSError *)error {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No Internet Connection" message:@"You don't have an internet connection! Please be aware that some pages might not load correctly." preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *Okay = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];

[alert addAction:Okay];

[self presentedViewController:alert animated:YES completion:nil];}

Answer Source

You've a typo — the method is presentViewController:animated:completion:, not present**ed**ViewController:animated:completion:. The 'present' is meant to be a verb, not an adjective — you're asking yourself to do something, not about something. So the error you've received is completely correct.

Change to:

[self presentViewController:alert animated:YES completion:nil];