Question

I´m really new in IOS programming with XCode 4.2. I already have a project (my 1st) with different functions, included UIWebView, and it works! Untill.. i tryed to add an alert when no network is around.

XCode tells me there are NO errors or issues. But when i Start the APP an go to the View where the WebView is:

.h:

#import "ViewController.h"

@interface ReservierungsformularBHV : UIViewController

{
    IBOutlet UIWebView *webview;
}

-(IBAction)refreshClicks:(id)sender;
@property (weak, nonatomic) IBOutlet UIWebView *webview;

@end

.m:

#import "ReservierungsformularBHV.h"

@interface ReservierungsformularBHV ()


@end

@implementation ReservierungsformularBHV
@synthesize webview;

- (void)viewDidLoad {

    NSURL *url = [NSURL URLWithString:@"http://www.bodega-online.com/tischreservierung      /mail_formular.php?ort=Bremerhaven"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webview loadRequest:req];
    [super viewDidLoad];
}



-(void)awakeFromNIB{  //IGNORE
    [self refreshClicks:self]; //IGNORE
}
-(IBAction)refreshClicks:(id)sender{//IGNORE
     [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.bodega-    online.com/tischreservierung/mail_formular.php?ort=Bremerhaven"]]];//IGNORE

     NSString *web = @"www.bodega-online.com/tischreservierung/mail_formular.php?ort=Bremerhaven";
     NSURL *url = [NSURL URLWithString:web];
     NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
     [webview loadRequest:requestUrl];

}
- (void)viewDidUnload
{
     [self setWebview:nil];
     [super viewDidUnload];
     // Release any retained subviews of the main view.

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Please check your internet connection"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
     [alert show];

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

XCode paused the simulator and shows me that:

2013-04-29 09:39:18.164 Bodega[3139:c07] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key reservierungsformularBHV.' * First throw call stack: (0x142c012 0x1251e7e 0x14b4fb1 0xcfde41 0xc7f5f8 0xc7f0e7 0xca9b58 0x3b3019 0x1265663 0x142745a 0x3b1b1c 0x2767e7 0x276dc8 0x276ff8 0x277232 0x282c25 0x4823a3 0x27fee3 0x280167 0x2801a7 0x5ec0a2 0x5ddb99 0x5ddc14 0x1265705 0x1992c0 0x3d5a64 0x1265705 0x1992c0 0x199258 0x25a021 0x25a57f 0x2596e8 0x1c8cef 0x1c8f02 0x1a6d4a 0x198698 0x2530df9 0x2530ad0 0x13a1bf5 0x13a1962 0x13d2bb6 0x13d1f44 0x13d1e1b 0x252f7e3 0x252f668 0x195ffc 0x1d72 0x1ca5) libc++abi.dylib: terminate called throwing an exception (lldb)

I really dont no what to do. Excuse my english, its long time ago.

Could anybody help me please? I cant find anything here, and what i found i used already in my project. Everything i do, still from tutorials. But i want to learn it!

Was it helpful?

Solution

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                            message:@"Please check your internet connection"
                                           delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];

You are setting the delegate to self, but you aren't defining the UIAlertView protocol in your ReservierungsformularBHV.h

Change

@interface ReservierungsformularBHV : UIViewController

to

@interface ReservierungsformularBHV : UIViewController <UIAlertViewDelegate>

and in your ReservierungsformularBHV.m add the following:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog("Dismissing alert");
}

Here's the link to the apple docs on UIAlertView: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

EDIT: in your .h file change

@interface ReservierungsformularBHV : UIViewController

to

@interface ReservierungsformularBHV : UIViewController <UIAlertViewDelegate, UIWebViewDelegate>

and change your viewDidLoad function to the following:

- (void)viewDidLoad {
    webview.delegate=self;
    NSURL *url = [NSURL URLWithString:@"http://www.bodega-online.com/tischreservierung      /mail_formular.php?ort=Bremerhaven"];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [webview loadRequest:req];
    [super viewDidLoad];
}

Let me explain what this does:

A webview has some so-called delegate functions, which are getting called in different cases. Your webViewDidFail-function was never called, because your webview didn't have the correct delegate assigned, which is done by defining the UIWebViewDelegate protocol in your .h file and setting the proper delegate in viewDidLoad to self. You could also have some other class handle your webview, but in this case you can use it like explained above.

Anyway, you should use the Reachability framework for checking on active internet connection and show the Alert if the device is not connected. There are a couple of examples how to do this on the web.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top