Reachability in iPhone OS 3.0 with 1 line of code

In iPhone OS 2.0 I used this:
if ((([NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/"]]!=NULL)?YES:NO) == NO) {}

In iPhone OS 3.0, they deprecated stringWithContentsOfURL, so I rewrote this as:
if ((([[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/"]]!=NULL)?YES:NO) == NO) {}

Then inside the {} of this IF statement, I display a UIAlertView.

The benefit to this code is you do not need to know if the device is in Airplane Mode or if the WiFi antennae is turned ON or OFF. Also, I would recommend deallocating or using autorelease to free up memory for the NSString.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.