Reachablity may not respond to +sharedReachability

Discussion in 'iOS Development' started by Collateral, Nov 13, 2009.

  1. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    And it doesnt. Actually it jusy crashes my app. Ive added "Reachability.h/m to my project, imported all of the correct files and added all of the correct frameworks.

    [OBJC]
    if([[Reachability sharedReachability] internetConnectionStatus] == NotReachable){
    UIAlertView *connectionAlert = [[UIAlertView alloc] init];
    [connectionAlert setTitle

    Please Register or Log in to view images

    "Error"];
    [connectionAlert setMessage

    Please Register or Log in to view images

    "Say Stuff."];
    [connectionAlert setDelegate:self];
    [connectionAlert setTag:1];
    [connectionAlert addButtonWithTitle

    Please Register or Log in to view images

    "Back"];
    [connectionAlert show];
    [connectionAlert release];
    }
    //else if (remoteHostStatus == ReachableViaWiFiNetwork) { }
    //else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { }[/OBJC]



    Here is the console output:

    Code:
    [Session started at 2009-11-13 09:47:16 -0500.]
    2009-11-13 09:47:24.964 BlackJackV2[33133:207] <SKProductsRequest: 0x3b034b0>: Failing immediately in simulator
    2009-11-13 09:47:24.967 BlackJackV2[33133:207] WARNING: SKPaymentQueue does not work in the simulator
    2009-11-13 09:47:26.548 BlackJackV2[33133:207] VIEWDIDLOAD: Started Intro Music
    2009-11-13 09:47:28.828 BlackJackV2[33133:207] *** +[Reachability sharedReachability]: unrecognized selector sent to class 0x13b08
    2009-11-13 09:47:28.829 BlackJackV2[33133:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Reachability sharedReachability]: unrecognized selector sent to class 0x13b08'
    2009-11-13 09:47:28.842 BlackJackV2[33133:207] Stack: (
        31564891,
        2469537033,
        31947003,
        31516278,
        31368898,
        48786,
        3352470,
        5250384,
        3360192,
        11166,
        2831449,
        3238818,
        3247555,
        3242767,
        2936371,
        2844700,
        2871477,
        39309009,
        31349632,
        31345736,
        39303053,
        39303250,
        2875395,
        8564,
        8418
    )
    


    The product request errors are because im not done configuring the in-app purchase yet.... ignore those.


    Ive Googled this to no end and tried a million different things and looked at the docs. Can someone explain to me how this works? Not just give me the code?

    Thanks.
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I assume you are referring to a class in the Apple-supplied sample app "Reachability". (You need to be more specific). In that case, the warnings that gcc is generating (that you must have ignored) and the 'unrecognized selector' message in the debugger are correct: the aforementioned sample app's "Reachability" class contains no such +[sharedReachability] method. (nor a -[internetConnectionStatus] method). That nomenclature implies a singleton design pattern, which you could implement yourself.

    If you modified the code, or this is something entirely different, post it or link us to it, as we can't debug code without reading it.

    EDIT: The code following the conditional evaluation in the if-statement is useless for our purposes; we need to see the code (even the interface section would be a start) for the class.
  3. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Yea. I am using the sample in its entirety, non-modified.

    The apple agreement says that if you have to connect to the internet, the HIG says that you have to check to see if your connected, and if you arn't, display an alert to the user. So i looked up how to do that, and the internet told me what i have above.

    I couldnt find the sharedReachability method either, but i thought that i was just wrong because people on the internet said it worked. (Maybe that was an older version of the Reachability class?)

    But basically I need to know how to check to see if the device is connected to a network either through wifi or a carrier signal.

    Sorry for not being specific.
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You can use the code in the "Reachability" class, just don't call methods that don't exist

    Please Register or Log in to view images

    (pardon the double-negative). Reading the header should be enough to get you going.
  5. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Alright I just got it. Thanks.


    EDIT: for anyone wondering... here you go:

    [OBJC]-(void)networkCheck{
    Reachability *curReach = [[Reachability reachabilityForInternetConnection] retain];
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    {
    case NotReachable:
    {
    UIAlertView *connectionAlert = [[UIAlertView alloc] init];
    [connectionAlert setTitle

    Please Register or Log in to view images

    "Error"];
    [connectionAlert setMessage

    Please Register or Log in to view images

    "myApp was not able to reach the host. Please check your network conenction."];
    [connectionAlert setDelegate:self];
    [connectionAlert setTag:1];
    [connectionAlert addButtonWithTitle

    Please Register or Log in to view images

    "Back"];
    [connectionAlert show];
    [connectionAlert release];
    NSLog(@"NETWORKCHECK: Not Connected");
    break;
    }
    case ReachableViaWWAN:
    {
    NSLog(@"NETWORKCHECK: Connected Via WWAN");
    break;
    }
    case ReachableViaWiFi:
    {
    NSLog(@"NETWORKCHECK: Connected Via WiFi");
    break;
    }
    }
    }
    [/OBJC]
  6. devlinford

    devlinford New Member

    Joined:
    May 27, 2010
    Messages:
    2
    Likes Received:
    0
    Hey Collateral!

    I have also imported the Reachibilit .h and .m files.

    I have put your above code in my AppDelegate.m in the ApplicationDidFinishLaunching portion of code?

    However I am getting an error of "networkCheck" being undeclared. I checked in the Reachibility.h and Reachibility.m files and there is no mention of this...

    What am I missing...

    Thanks in advance!


    DF
    ------------------double post merged------------------
    I actually have it building now...

    I put it outside of the "ApplicationDidFinishLaunchingWithOptions" and it builds, but it doesnt do anything. When there is no network, nothing happens...

    Help?!?
  7. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    sad part about that, curReach will leak because you use retain. Maybe you don't need retain?
  8. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    In this case the retain call is unnecessary; the Reachability reference is used only in the next line, and it doesn't need to be a local var anyway ( [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] )

    Retaining an object is useful in that it allows you "own" the object. You do not "own" an autoreleased object returned from a method. You can still use such an object, but to ensure it sticks around for as long as you may need it, a retain, and later a release call should be made.
  9. devlinford

    devlinford New Member

    Joined:
    May 27, 2010
    Messages:
    2
    Likes Received:
    0
    Hey Axis...

    You seem to have a good coding head on your shoulders...

    Would you be able to offer some assistance?

    I am quite new at iPhone OS dev, and I am really having a hard time with this "no network alert"...I have read thru the "Reachability" example and it makes my head spin...I am looking for a simple implementation or adaptation of this function in for my app - Which is a TabBar/Nav Controller app with 5 UIWebViews.

    The first UIWebView is displayed at launch so I logically figured that would be the be the best place for the above code to go. That said, I have added this code to my firstViewController.m file right after the "viewDidLoad" call. Seen below - I am using place holder mobile sites for now:


    #import "FirstViewController.h"
    #import "Reachability.h"


    @implementation FirstViewController

    -(void)viewDidLoad {

    //declaring the subview of the UIWebView
    [newsweb addSubview: activityIndicator];

    [newsweb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString

    Please Register or Log in to view images

    "http://m.ufc.com/"]]];
    timer = [NSTimer scheduledTimerWithTimeInterval

    Please Register or Log in to view images

    1.0/2.0) target:self selector

    Please Register or Log in to view images

    selector(loading) userInfo:nil repeats:YES];


    }

    -(void)networkCheck{
    Reachability *curReach = [[Reachability reachabilityForInternetConnection] retain];
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    {
    case NotReachable:
    {
    UIAlertView *connectionAlert = [[UIAlertView alloc] init];
    [connectionAlert setTitle

    Please Register or Log in to view images

    "Error"];
    [connectionAlert setMessage

    Please Register or Log in to view images

    "myApp was not able to reach the host. Please check your network conenction."];
    [connectionAlert setDelegate:self];
    [connectionAlert setTag:1];
    [connectionAlert addButtonWithTitle

    Please Register or Log in to view images

    "Back"];
    [connectionAlert show];
    [connectionAlert release];
    NSLog(@"NETWORKCHECK: Not Connected");
    break;
    }
    case ReachableViaWWAN:
    {
    NSLog(@"NETWORKCHECK: Connected Via WWAN");
    break;
    }
    case ReachableViaWiFi:
    {
    NSLog(@"NETWORKCHECK: Connected Via WiFi");
    break;
    }
    }
    }​

    I have also imported the Reachability.H/.M files and imported the SystemConfiguration.framework too!

    Currently this builds with NO errors. When I have a connection, it loads and runs as it should...When I disable my connection - the TabBar/Nav Controller loads a blank UIWebView - my activityIndicator spins for 1 second then disappears and no alert is called?!?!

    Can you spot what I am doing wrong here, does something have to do done in the Reachability files before this would work?

    I thank you in advance for your time,


    Dev
  10. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    where you are calling for [self networkCheck];? I found no trace of reachabilityForInternetConnection is Apple's supplied Reachablity.

Share This Page