Use NSDictionary 'initWithContentsOfURL' instead Reachability.h

1.7k views Asked by At

I would like to better understand the functioning of of initWithContentsOfURL of NSDictionary.
This function manages by itself the failure of a connection?
From the initWithContentsOfURL of NSDictionary reference:

- (id)initWithContentsOfURL:(NSURL *)aURL

Return Value

An initialized dictionary-which Might be different than the original-that contains the receiver at aURL dictionary, or nil if there is an error or if the contents of the resource are an invalid representation of a dictionary.

Ok, but does not specify whether the url passed is valid or not.

But since i'm sure my plist is well-formatted, i could use the method in question to see if the connection is available or not, instead of using the Reachability.h. It is of course just to understand if a data connection is available, not to understand what kind of connection is active(e.g. WiFi, etc). I say this because if i do a simple test like this in airplane mode, [dict count]; always returns 0.

NSURL * plist = [NSURL URLWithString: @ "http://www.example.com/example.plist"];
NSDictionary * dict = [[[NSDictionary alloc] initWithContentsOfURL:plist] autorelease];
  if ([dict count] == 0) {
      //no connection
  }

TIA.

1

There are 1 answers

5
logancautrell On BEST ANSWER

Yes you can do that. Keep in mind that [[NSDictionary alloc] initWithContentsOfURL:plist] is a synchronous blocking call. If you block the main thread too long then you'll get 0x8BADF00D, and the watch dog will kill your process.