A
A
Alexander Zarochintsev2014-03-29 12:40:46
Objective-C
Alexander Zarochintsev, 2014-03-29 12:40:46

How to send a notification about the loss of Internet connection through the entire application?

Good time of the day! The question is a little silly, but nonetheless. I have an application that can't work without internet connection, I found SDReachability , but the problem is that I can't figure out how to use it, I declared it in AppDelegate, how to show a notification on all screens when the internet goes down?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Morozov, 2014-03-29
@Kovalskiy

according to the link on github there is an example of how to track that the Internet is gone
here (I slightly changed the example):

@interface MyViewController ()

@property (strong, nonatomic) SDReachability *reachability;

@end


@implementation MyViewController

//...

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self monitorReachability];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear];
    [self stopMonitorReachability];
}

- (void)monitorReachability
{
    self.reachability = [SDReachability reachabilityWithTarget:self action:@selector(reachabilityChanged:)];
}

- (void)stopMonitorReachability
{
    self.reachability = nil;
}

- (void)reachabilityChanged:(SDReachability *)reachability
{
    switch (reachability.reachabilityStatus)
    {
        case SDNotReachable:
        {
            NSLog(@"Connection lost");
           UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Connection lost" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            // тут делаеш с view всё что захочешь
            break;
        }
        case SDReachableViaWiFi:
            NSLog(@"Connected via WiFi");
            break;

        case SDReachableViaWWAN:
            NSLog(@"Connected via WWAN");
            break;
    }
}

@end

do this for each ViewController or you can do it once per RootViewController:
@interface MyRootViewController ()

@property (strong, nonatomic) SDReachability *reachability;

@end


@implementation MyRootViewController

//...

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self monitorReachability];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear];
    [self stopMonitorReachability];
}

- (void)monitorReachability
{
    self.reachability = [SDReachability reachabilityWithTarget:self action:@selector(reachabilityChanged:)];
}

- (void)stopMonitorReachability
{
    self.reachability = nil;
}

- (void)reachabilityChanged:(SDReachability *)reachability
{
    switch (reachability.reachabilityStatus)
    {
        case SDNotReachable:
        {
            NSLog(@"Connection lost");
           UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Connection lost" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];

           [self.navigationController popToRootViewController]; // убираем все контроллеры из стека
           // тут модифицируешь self.view - например добавляеш на неё label "Connection Lost"
            break;
        }
        case SDReachableViaWiFi:
            NSLog(@"Connected via WiFi");
            break;

        case SDReachableViaWWAN:
            NSLog(@"Connected via WWAN");
            break;
    }
}

@end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question