0

我将此DMRNotificationAPPLE Reachability一起使用,以便在没有 Internet 连接时向我的用户显示通知。一切正常,除了通知显示两次,我真的不明白为什么。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];

    self.wifiReachability = [Reachability reachabilityForLocalWiFi];
    [self.wifiReachability startNotifier];
    [self updateInterfaceWithReachability:self.wifiReachability];
}

- (void) reachabilityChanged:(NSNotification *)note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}

- (void)updateInterfaceWithReachability:(Reachability *)reachability {
    NetworkStatus netStatus = [reachability currentReachabilityStatus];

    switch (netStatus) {
        case NotReachable: {
            NSString *title = @"No Internet Connection";
            NSString *subTitle = @"It seems that you are not connected to internet. ;-(";

            self.notificationView = [[DMRNotificationView alloc] initWithTitle:title subTitle:subTitle targetView:self.view];
            [self.notificationView setTitleFont:[UIFont fontWithName:@"MarkerFelt-Thin" size:20.0]];
            [self.notificationView setSubTitleFont:[UIFont fontWithName:@"MarkerFelt-Thin" size:13.0]];
            [self.notificationView setHideTimeInterval:0.0];     // Prevents the notification view to automatically dismiss
            [self.notificationView setDidTapHandler:^() {}];
            [self.notificationView showAnimated:YES];
            break;
        }
        case ReachableViaWWAN: {
            [self.notificationView dismissAnimated:YES];
        }
        case ReachableViaWiFi: {           
            [self.notificationView dismissAnimated:YES];
        }
    } 
}
4

0 回答 0