0

I have a NSURL like so:

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

but if there is no internet connect or phone signal, my app crashes. Is there away to test the connect first before use?

Would I have to do try and catch? I have been reading about NSURLConnection, but I am unclear on how it works. Any ideas?

The error I am getting is Thread 6:signal SIGABRT I have no idea what this means, I have tried all the examples below and none worked.

I added this:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    // receivedData is declared as a method instance elsewhere

    [connection release];

    // inform the user
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];
    [didFailWithErrorMessage release];

}

and I unplugged my Mac from the internet and ran my app on the simulator and this appears Thread 6:signal SIGABRT

I have used this code below

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
        });
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
        });

Works when I am connected to the enter, but when I turn off my internet I get this error and the else alert does not appear

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
Mike Abdullah
  • 14,782
  • 2
  • 47
  • 73
user1269625
  • 2,831
  • 24
  • 74
  • 107
  • What is causing the crash in your app? That should not be the case, event is you do not have internet. – Ludovic Landry Jul 15 '13 at 00:59
  • Well its not really crashing, I am just looking for away to test the connection of NSURL and if it fails display Alert message. – user1269625 Jul 15 '13 at 01:02
  • You should just create your connection and handle errors correctly (e.g. check the return values, handle the [`didFailWithError`](http://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40009947-CH1-SW14), etc.). I think writing a `didFailWithError` will do it for you. – Rob Jul 15 '13 at 01:04
  • I tried `didFailWithError` and this message popped up `Thread 6:signal SIGABRT` do I need to call `connection` somewhere? – user1269625 Jul 15 '13 at 03:08

3 Answers3

0

You can use the sample code provided by Apple to test the reachability:

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

You initialize the Reachability with the hostname you want, then you can:

  • start/stop the notifier and you will receive notifications that indicates if when the reachability to this host change

  • or just get the current reachability status

Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus == NotReachable) {
    // You have internet
} else {
    // You do not have internet
}
Ludovic Landry
  • 10,946
  • 9
  • 45
  • 79
  • Do I need to import and @implementation something? I am getting errors on this line `Reachability *reachability = [Reachability reachabilityForInternetConnection];` and this line `NetworkStatus internetStatus = [reachability currentReachabilityStatus];` both errors say `undeclared identifier (Reachabilty or NetworkStatus)` – user1269625 Jul 15 '13 at 01:22
  • Download and add to your project the Reachability class, I gave you the link... You have to import Reachability.h – Ludovic Landry Jul 15 '13 at 01:53
  • This did not work, when I was connected to the internet it would goto the else. if I disconnected from the internet and ran my app on the simulator i get this message `Thread 6:signal SIGABRT` – user1269625 Jul 15 '13 at 03:07
  • This is working, please do some research... http://stackoverflow.com/questions/13735611/check-for-internet-connection-ios-sdk – Ludovic Landry Jul 15 '13 at 03:47
  • I have updated my question, when I use the last bit of code and turn off my internet on my computer and run the simulator, I get this error `erminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'` and the alert does not appear. – user1269625 Jul 15 '13 at 06:18
0

One thing I like to do, is check the connection with a different NSURL and change a BOOL for connection or not. I made a method like this..

- (void)checkConnection
{
NSURL *checkURL = [NSURL URLWithString:@"http://www.apple.com"];
NSData *data = [NSData dataWithContentsOfURL:checkURL];

if (data)
{
    _connected = YES;
}
else
{
    _connected = NO;
}
}

Now I have a BOOL _connected letting me know if I am connected or not. Then in any method that you need to make sure you are connected first do something like this..

- (void)viewWillAppear:(BOOL)animated
{
[self checkConnection];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

if (_connected == YES)
{
    [self fetchTweets];
    [self reFresh];
    [self.tableView reloadData];
}else
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet"
                                                      message:@"You must have an internet connection."
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
    [self.refreshControl endRefreshing];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    return;
}
}

That's what I have done for some Apps I have written. It's quick and you don't need a ton of code or imports. Or you can take a look into NSURLConnection and NSURLRequest. Combined they have methods that let you show warnings and such if there is no data. It is what Rob was talking about. Just a thought!

Douglas
  • 2,514
  • 3
  • 27
  • 42
  • @downvoter, why not put a little explanation behind that so we can all learn? – Douglas Jul 18 '13 at 23:57
  • Don't do this. It's wasteful, and the state of the internet connection can easily change at any time after you've tried this "check". – Mike Abdullah Jul 20 '13 at 08:58
  • @Mike Abdullah, I see what you mean about the connection can change, but what do you mean wasteful. Wasteful, because it can change or because its bad programming? Thanks. – Douglas Jul 20 '13 at 11:59
  • It wastes bandwidth and battery power accessing an unrelated site, and then doesn't give you a 100% correct answer. Instead just access the real target, and be properly prepared to handle failures – Mike Abdullah Jul 20 '13 at 22:05
  • @Mike Abdullah, thanks for the info, I will make sure to get rid of it in my programming. . – Douglas Jul 21 '13 at 01:29
0

Tony Million's fork of Apple's Reachability code is a great, block-based solution to this problem: https://github.com/tonymillion/Reachability

// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// set the blocks 
reach.reachableBlock = ^(Reachability*reach)
{
    NSLog(@"REACHABLE!");
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];
jaredsinclair
  • 12,547
  • 4
  • 33
  • 54