Best way to implement RKReachabilityObserver in RestKit

I wrote a tab-based application in Xcode / RestKit and am trying to use RKReachabilityObserver to determine if I can connect to the Internet on my device.

Ideally, I would like to have one reachability variable throughout my application (if possible), but currently my implementation matches the code below and doesn't work very well when replicating to my 4 tabs.

If anyone has suggestions on a better way to do this, I will be very grateful for your comments.

view.h

@property (nonatomic, retain) RKReachabilityObserver *observer;

View.m

@interface AppViewController()
{
    RKReachabilityObserver *_observer;
}
@property (nonatomic) BOOL networkIsAvailable;
@synthesize observer = _observer;

-(id)initWithCoder:(NSCoder *)aDecoder {

    if ((self = [super initWithCoder:aDecoder])) {

        self.observer = [[RKReachabilityObserver alloc] initWithHost:@"mydomain"];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(reachabilityChanged:)
                                                     name:RKReachabilityDidChangeNotification
                                                   object:_observer];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // determine network availability
    if (! [_observer isReachabilityDetermined]) {
        _networkIsAvailable = YES;
    }
    else
    {
        _networkIsAvailable = NO;
    }

    _text.returnKeyType = UIReturnKeyDone;
    _text.delegate = self;
}

- (void)reachabilityChanged:(NSNotification *)notification {
    RKReachabilityObserver* observer = (RKReachabilityObserver *) [notification object];
    if ([observer isNetworkReachable]) {
        if ([observer isConnectionRequired]) {
            _networkIsAvailable = YES;
            NSLog(@"Reachable");
            return;
        }
    } 
    else 
    {
        _networkIsAvailable = NO;
        NSLog(@"Not reachable");
    }
}

then anywhere, in my opinion, I just do ....

if (_networkIsAvailable == YES)
    {...

I implemented this in several ways (which seems to be causing the problem.

What is the proposed approach for a multi-tasking application?

+5
2

[RKClient sharedClient] (reachabilityObserver). .

if ([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] && [[RKClient sharedClient] isNetworkReachable]) {
    ....
}

RKReachabilityObserver ( - )

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reachabilityStatusChanged:) 
                                                 name:RKReachabilityDidChangeNotification object:nil];
+18

RestKit 0.20 . :

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[RemoteTools serverUrl]];
[manager.HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if (status == AFNetworkReachabilityStatusNotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
                                                        message:@"You must be connected to the internet to use this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}];
+2

All Articles