0

Xcode 4.4 で到達可能性を使用して、インターネットに接続していない場合にユーザーに警告しようとしています。私の最初のView Controllerには、テーブルをロードするボタンがあります(オンラインの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 を使用してインターネットを確認する必要がありますか?

4

1 に答える 1

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
}
于 2012-08-23T05:36:28.403 に答える