使用可达性通知用户有关Internet连接的信息

时间:2012-08-23 05:20:53

标签: objective-c ios reachability xcode4.4

我正在尝试在Xcode 4.4中使用可访问性来提醒用户他/她是否未连接到互联网。我的初始视图控制器有一个按钮,用于加载一个表(从在线plist填充)。我在Stack Overflow中遵循了一些示例,但无法使其正常工作。这是我的.h文件的片段:

#import <UIKit/UIKit.h>
#import "Reachability.h"

@interface MainPageViewController : UIViewController
{
  Reachability *internetReachable;
  Reachability *hostReachable;
}

-(void) checkNetworkStatus: (NSNotification *)notice;
@property BOOL internetActive;
@property BOOL hostActive;

@end

这是我的.m文件:

#import "MainPageViewController.h"
#import "Reachability.h"

@implementation MainPageViewController
@synthesize internetActive;
@synthesize hostActive;

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

- (void) viewWillAppear:(BOOL)animated
  {
    [[NSNotificationCenter defaultCenter] addObserver: self selector:
    @selector(checkNetworkStatus:) name: kReachabilityChangedNotification object: nil];

    internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];

    hostReachable = [Reachability reachabilityWithHostname: @"sites.google.com"];
    [hostReachable startNotifier];
  }

- (void) checkNetworkStatus:(NSNotification *)notice
  {
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];

    if((internetStatus == NotReachable) && (hostStatus == NotReachable))
    {
       UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Network Error!"
       message: @"You are not connected to the internet!" delegate: self
       cancelButtonTitle: @"Ok" otherButtonTitles: nil];
       [internetAlert show];
       self.internetActive = NO;
       self.hostActive = NO;
    }
  }

-(void) dealloc
  {
    [[NSNotificationCenter defaultCenter] removeObserver: self];
  }

- (void)viewDidLoad
  {
    [super viewDidLoad];
// Do any additional setup after loading the view.
  }

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

- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation
   {
   return YES;
   }

@end

由于我使用的是按钮,在导航到下一页之前,是否应该使用IBAction检查互联网?

1 个答案:

答案 0 :(得分:0)

您可以在方便的地方查看互联网。考虑在viewDidLoad或didFinishLaunching方法中检查它,或者在您需要互联网连接之前检查它。拥有“互联网可用吗?”可能不那么方便。没有任何额外动作的按钮。

这是一个无论您是否已连接都将返回的功能。我通常在应用程序的过程中经常运行它(或者在每次请求之前,如果我是偏执狂)。

- (BOOL) connectedToNetwork
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    BOOL internet;
    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
        internet = NO;
    } else {
        internet = YES;
    }
    return internet;
} 

然后您可以像这样使用它(假设您从当前对象引用它):

if(![self connectedToNetwork]) {
    //Do whatever you need to if you're not connected
} else {
    // You're connected so let the games begin
}