I have created an error message that should display if the user can not connect to google.com by eithe开发者_高级运维r WI-FI or WWAN. I do not get any coding errors, so nothing seems to be wrong. I am using a UIWebview that goes to twitter.com. The issue may be that I am trying to get the error to display when the view loads and not the UIWebview, but I am not sure. Another issue that I am having is that I can not get "reachability" recognized as a name in the TwitterViewController.h file. Here is the code:
TwitterViewController.h
#import <UIKit/UIKit.h>
#import "Reachability.h"
@interface TwitterViewController : UIViewController {
Reachability *reachability;
}
@end
TwitterViewController.m
#import "TwitterViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#include <netinet/in.h>
#import "TwitterView.h"
@implementation TwitterViewController
- (void)viewDidLoad {
[super viewDidLoad];
Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
UIAlertView *NEalert = [[UIAlertView alloc] initWithTitle: @"Error"
message: @"Could not load the webpage. Please check your internet connection."
delegate: nil
cancelButtonTitle: @"Dismiss"
otherButtonTitles: nil];
[NEalert show];
[NEalert release];
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
[super dealloc];
}
#pragma mark -
#pragma mark Initialisation
- (id)init {
self = [super init];
if (self) {
self.hidesBottomBarWhenPushed = YES;
UINavigationBar objects
self.title = @"Twitter";
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
return self;
}
#pragma mark -
#pragma mark Action Methods
- (void)twitterBUTTONAction {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"NFAGaming's Twitter"
message: @"To follow NFAGaming on twitter, go to: twitter.com/NFAGaming"
delegate: nil
cancelButtonTitle: nil
otherButtonTitles: @"Ok", nil];
[alert show];
[alert release];
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
@end
EDIT: This is what ended up working perfectly for me
{{Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if (internetStatus == NotReachable) {
UIAlertView *tNEalert = [[UIAlertView alloc] initWithTitle: @"Error"
message: @"Internet Connection Required"
delegate: self
cancelButtonTitle: @"Dismiss"
otherButtonTitles: nil];
[tNEalert show];
[tNEalert release];
}
else {
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
}}
I used to use reachability like this:
self.reachability = [Reachability reachabilityForInternetConnection];
if ( [self.reachability currentReachabilityStatus] == NotReachable) {
UIAlertView *noConnectionAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",@"Alert Title")
message:@"Internet connection is required." delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[noConnectionAlert show];
[noConnectionAlert release];
}
Use should use reachabilityWithHostName:
only if you need to determine reachability to a specific host. See if this will work.
精彩评论