Best way to implement RKReachabilityObserver in Re

2019-02-01 14:56发布

问题:

I have written a tab based application in Xcode/RestKit and am attempting to use the RKReachabilityObserver to determine Internet connectivity on the device.

Ideally I'd like to have a single reachability variable throughout my application (if this is possible) but currently my implementation is as per the code below and does not work very well when replicated over my 4 tabs.

If anybody has any suggestions of a better way to do this, I'd really appreciate 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 view, I simply do....

if (_networkIsAvailable == YES)
    {...

I have implemented this over multiple views (which seems to be causing the problem.

What is the suggested approach for a multiple-view application?

回答1:

The [RKClient sharedClient] singleton already has a property for that (reachabilityObserver). Feel free to use that one.

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

You can also subscribe to RKReachabilityObserver notifications (if you want to take any action when reachability status changes)

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


回答2:

Here is some changes in RestKit 0.20 and later. The code of reachability block should looks like:

    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];
    }
}];