2011-09-24 57 views
0

我创建了一条错误消息,如果用户无法通过WI-FI或WWAN连接到google.com,应该显示该消息。我没有收到任何编码错误,所以没有任何错误。我正在使用twitter.com的UIWebview。这个问题可能是我试图在加载视图时显示错误,而不是UIWebview,但我不确定。我遇到的另一个问题是,我无法在TwitterViewController.h文件中将“可访问性”识别为名称。下面是代码:iOS可达性错误将不会显示

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 

编辑: 这是结束了,我可以正常使用

{{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]; 

     } 
    }} 

回答

0

我用使用像这样的可达性:

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]; 
} 

仅当需要确定是否可以访问特定主机时,才应使用reachabilityWithHostName:。看看这是否会奏效。

+0

非常感谢,如果我使用reachabilityForInternetConnection,它可以很好地工作。 – user962947