How do I repeat a Reachability test until it works

后端 未结 1 432
盖世英雄少女心
盖世英雄少女心 2021-01-03 15:45

I have an initial tableviewcontroller which is executing a reachability check. This is working without a problem within the viewDidLoad, however

相关标签:
1条回答
  • 2021-01-03 16:34

    How should you use Reachability?

    • Always try your connection first.
    • If the request fails, Reachability will tell you why.
    • If the network comes up, Reachability will notify you. Retry the connection then.

    In order to receive notifications, register for the notification, and start the reachability class from 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
    

    To answer the notification:

    - (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
    }
    

    If you rather use blocks instead, use KSReachability.

    0 讨论(0)
提交回复
热议问题