0

I have the following code in my AppDelegate.m - the result of the NSLog is always (null) and thus the condition for no reachability is never fired. I would like to know why this is happening/what I am doing incorrectly.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [IKRestKitManager configureRestKit];

    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];

    [self prepareForLogin];

    return YES;
}

#pragma mark - onstart

- (void)prepareForLogin {

    if ([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] && ![[RKClient sharedClient] isNetworkReachable]) {
        UIAlertView *reachAV = [[UIAlertView alloc] initWithTitle:@"Cannot connect to Internet" message:@"iK9 cannot reach the Internet. Please be sure that your device is connected to the Internet and try again." delegate:self cancelButtonTitle:@"Retry" otherButtonTitles:nil];
        reachAV.tag = 0;
        [reachAV show];
    }

    NSLog(@"%@",[[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined]);


    if (![IKUserController loggedInUser]) {
        IKLoginViewController *loginVC = [[IKLoginViewController alloc] init];
        loginVC.scenario = SCENARIO_NEW;
        [self.window.rootViewController presentModalViewController:loginVC animated:YES];
    }
}
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126

3 Answers3

5

According to th documentation for RKReachabilityObserver:

When initialized, RKReachabilityObserver instances are in an indeterminate state to indicate that reachability status has not been yet established. After the first callback is processed by the observer, the observer will answer YES for reachabilityDetermined and networkStatus will return a determinate response.

You need to wait until the reachability status has been determined before checking if a connection is available. This is why your first if statement is not triggering.

To monitor this change, setup a notification observer (from this Stack Overflow question):

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(reachabilityStatusChanged:) 
                                      name:RKReachabilityDidChangeNotification object:nil];
Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
2

BOOLean values are not objects, so %@ is not their string literal. They are actually unsigned char, but you NSLog them with:

NSLog([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] ? @"Yes" : @"No");
CodaFi
  • 43,043
  • 8
  • 107
  • 153
1

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];
    }
}]
CTiPKA
  • 2,944
  • 1
  • 24
  • 27