2

到達可能性チェックを実行している最初の tableviewcontroller があります。これは 内で問題なく動作していますがviewDidLoad成功するまで接続を再試行する正しい方法を知りたいです。私の実装ファイルの関連コードは以下のとおり[self ViewDidLoad]です。接続がダウンしている場合に挿入しようとしましたが、これはアプリをループに設定し (接続失敗NSLogメッセージを返す) UIAlertView、.

- (void)viewDidLoad
{
    [super viewDidLoad];

    if(![self connected])
    {
        // not connected
        NSLog(@"The internet is down");
        UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection      Error" message:@"There is no Internet Connection" delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:nil, nil];
        [connectionError show];
        [self viewDidLoad];
    } else
    {
        NSLog(@"Internet connection established");
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
        [btn addTarget:self action:@selector(infoButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]    initWithCustomView:btn];
        [self start];
    }
}
4

1 に答える 1

3

到達可能性をどのように使用する必要がありますか?

  • 常に最初に接続を試してください。
  • リクエストが失敗した場合、Reachability が理由を教えてくれます。
  • ネットワークが起動すると、Reachability が通知します。その後、接続を再試行します。

通知を受け取るには、通知を登録し、Apple から到達可能性クラスを開始します。

@implementation AppDelegate {
    Reachability *_reachability;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector: @selector(reachabilityChanged:)
     name: kReachabilityChangedNotification
     object: nil];

    _reachability = [Reachability reachabilityWithHostName: @"www.apple.com"];
    [_reachability startNotifier];

    // ...
}

@end

通知に回答するには:

- (void) reachabilityChanged: (NSNotification *)notification {
    Reachability *reach = [notification object];
    if( [reach isKindOfClass: [Reachability class]]) {
    }
    NetworkStatus status = [reach currentReachabilityStatus]; 
    NSLog(@"change to %d", status); // 0=no network, 1=wifi, 2=wan
}

代わりにブロックを使用する場合は、KSReachabilityを使用してください。

于 2013-04-06T18:59:19.400 に答える