Modal View when Offline
First up, many thanks for the modal webview - fantastic and exactly what my application needed!
Only one glitch for me - when offline the modal window just keeps its indicator spinning and doesn't time out. Not feeding back to the user when offline is a common cause for app rejection so should probably be resolved - say through a timeout and error message if the connection fails.
Good point! :) Thanks for the feedback. :)
A quick fix for this - add to ModalViewController.m. It makes an additional call to the Internet and could be refined - I was going for something that didn't touch the existing code.
-(void)viewDidAppear:(BOOL)animated {
NSString* link = @"http://google.com";
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:0 timeoutInterval:5];
NSURLResponse* response=nil;
NSError* error=nil;
NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data == NULL) {
NSLog(@"Not connected");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Page unavailable - you don't seem to be connected to the internet right now."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[[self delegate] done:self];
} else {
NSLog(@"Connected");
}
}
Thanks @davidrsim! Unfortunately we weren't able to follow up on this, so thanks for the additional detail! :)
Has this been added to Baker? I'm still getting a timeout. If not, does the code above still work? @davidrsim, can you explain where you added your code exactly?
@tstegart When something gets solved or added, the topic is closed, so no we weren't able to add this yet. We were working on refactoring of the code as you can see from the other issue.
Thanks. Yeah, I noticed all the work on the code the last week, it looks great! Hopefully @davidrsim can chime in with where he placed his code.
Hi,
I added it below the loadView section, so...
self.view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[self.view sizeToFit];
// ****** Attach
[self.view addSubview:toolbar];
[self.view addSubview:webView];
}
-(void)viewDidAppear:(BOOL)animated {
NSString* link = @"http://google.com";
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:0 timeoutInterval:5];
NSURLResponse* response=nil;
etc... Line 154 on mine, I think!
Thanks @davidrsim. I tried it, but I'm getting an error on this line:
[[self delegate] done:self]; - Instance method '-done' not found (return type defaults to "id")
It's a warning rather than an error (you'll also see it in the (IBAction)dismissAction: section too) - it should still compile and work ok. (At least it does for me).
It compiles, but throws an error when I turn off wi-fi on the iPad and try and test it.
-[RootViewController done:]: unrecognized selector sent to instance 0x1be260 2012-07-16 19:02:37.693 ONB Magazine[2260:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController done:]: unrecognized selector sent to instance 0x1be260' *** First throw call stack: (0x3603488f 0x37289259 0x36037a9b 0x36036915 0x35f91650 0x23cd9 0x30cebd3d 0x30d45f2d 0x30d74de7 0x30da5761 0x30d43d77 0x30d43bfb 0x30cbec53 0x30cc457d 0x37da5c2f 0x36e2ce91 0x360072ad 0x35f8a4a5 0x35f8a36d 0x3234a439 0x30cd7e7d 0x228f 0x2240) terminate called throwing an exception(lldb)
I could't get the code above to work for me, so I snooped on the internet and found this link: http://stackoverflow.com/questions/9094830/testing-internet-connection-on-ipad-app-using-ios5
I added the example code to ModalViewController.m around line 188 just after webViewDidStartLoad. I then added in@davidrsim's warning code and tweaked the message a little. It seems to work just fine, although it could be improved. After you click on "OK," you're still stuck in modal view with a blank page and the spinner is still spinning. Adding another button to the warning to "Go Back" might be useful to take people back to the book page they were viewing before they opened modal view. Maybe this can be added to the next version of Baker.
Code below:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (![[error domain] isEqualToString:NSURLErrorDomain]) {
return;
}
// Show warning to user that device is not connected
NSLog(@"Not connected");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"The page you are trying to reach is unavailable - you don't seem to be connected to the internet right now."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
We have added a error message on the modal window with the commit 8bdffd7ccec440f52535fdc189b0b548dbb35ac2. The error appears when a user tries to load a page without internet connection.