Online and offline support for RestKit iOS applications Online and offline support for RestKit iOS applications ios ios

Online and offline support for RestKit iOS applications


You might use the Reachability Class to determine wether your client is offline or not. I use this great class very often in every project that requires an internet connection.

You simply start the notifier to a specific host. In all of your viewController you now just have to register methods to the NSNotificationCenter to set a BOOL isOnline for example.

Doing this practice you can do beautiful stuff in your app like overlaying the app with a smooth "Offline" message.

https://gist.github.com/1182373

EDIT

Heres one example from the login screen from one of my projects (sorry for this quantity of code but this is my complete implementation):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    //// Some stuff ////    [[Reachability reachabilityWithHostname:@"apple.com"] startNotifier];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];}- (void)reachabilityChanged:(NSNotification *)note{    if ([[note object] isReachable]) {        CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];        [scale setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:0.0], nil]];        [scale setDuration:0.3];        [scale setRemovedOnCompletion:NO];        [[offlineView layer] setTransform:CATransform3DMakeScale(0, 0, 1.0)];        [[offlineView layer] addAnimation:scale forKey:@"scale"];        [[offlineView layer] setTransform:CATransform3DIdentity];        [UIView animateWithDuration:0.3 animations:^{            [offlineView setAlpha:0];        } completion:^(BOOL finished){            if (finished) {                [offlineView removeFromSuperview];            }        }];        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];    }    else {        CGRect screenFrame = CGRectMake(0, 0, 320, 480);        offlineView = [[UIView alloc] initWithFrame:screenFrame];        [offlineView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];        [offlineView setAlpha:0];        offlineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];        [offlineLabel setFont:[UIFont fontWithName:@"Verdana" size:30.0]];        [offlineLabel setBackgroundColor:[UIColor clearColor]];        [offlineLabel setTextAlignment:UITextAlignmentCenter];        [offlineLabel setTextColor:[UIColor whiteColor]];        [offlineLabel setCenter:[offlineView center]];        [offlineLabel setText:@"OFFLINE"];        [offlineView addSubview:offlineLabel];        [[self window] addSubview:offlineView];        [UIView animateWithDuration:0.3 animations:^{            [offlineView setAlpha:1.0];        }];        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];    }}


Please check the version of RestKit; in the newer version of RestKit there is slightly changed approach of getting the cached data from managed object store.I don't have the example at this machine; but If you need help any more (since this a rather old question) please reply.