iOS 可达性错误不会显示

Posted

技术标签:

【中文标题】iOS 可达性错误不会显示【英文标题】:iOS Reachability error will not display 【发布时间】:2011-09-24 19:40:23 【问题描述】:

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

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

        
    

【问题讨论】:

【参考方案1】:

我曾经这样使用可达性:

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:。看看这是否可行。

【讨论】:

非常感谢,如果我使用reachabilityForInternetConnection,它可以完美运行。

以上是关于iOS 可达性错误不会显示的主要内容,如果未能解决你的问题,请参考以下文章

iOS 5.1 可达性

如何在 iOS 中测试可达性并重试连接?

iOS8 的可达性问题

ios - 可达性通知多个警报

iOS5需要ARC的可达性版本

需要 https 或 *** (ios) 的可达性类和主机名