tags:

views:

75

answers:

3

Hi all i update to ios 4 but in my app i use :

NSString *connected = [NSString string withContentofURL:[NSURL URLWithString:@"http://myurl.com/myFile]];

but now i get the following :

StringWithContentsofURL is deprecated ! 

I use this to test if connection is available.

What can i do ??

thanks

A: 

A little Googling will give you the answer here:

It has been replaced with stringWithContentsOfURL:encoding:error...

Source: http://stackoverflow.com/questions/2039203/what-is-the-stringwithcontentsofurl-replacement-for-objective-c

Also, in the future, please try to spell check your inquiries.

esqew
o really i'll take a look tanks and sorry
sarlin13
+1  A: 

As of iPhone OS2 (so this is not new) the

[NSString withContentsOfURL: (NSURL*) url]

method has been replaced with

+ (id)stringWithContentsOfURL: (NSURL *)url encoding: (NSStringEncoding)enc error: (NSError **)error

Here's an example using the new signature:

NSError* error = nil;
NSURL* url = [NSURL urlWithString: @"www.google.com"];
NSString* stringForUrlPath = [NSString stringWithContentsOfURL: url 
                                                      encoding: NSUTF8StringEncoding 
                                                         error: &error];

See this for your options for NSStringEncoding.

sdolan
yea but encoding ?
sarlin13
What kind of websites are you visiting? Most likely NSUTF8StringEncoding should serve you just fine.
sdolan
A: 

Use

 NSError* error;
 NSString *string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

instead. Apple shouldn't have included stringWithContentsOfURL: (without encoding) to start with. It has been deprecated on OS X for a long time, because that was the cause of a lot of headaches for non-English speakers.

That said, don't download something to test the connectivity. Instead, use Reachiability.

Yuji
Reachability will only tell you if you have 3g or wifi connectivity, not if you can reach a site. The best check if an address is reachable would be to implement a pinging class. Apple has an example of this here: http://developer.apple.com/mac/library/samplecode/SimplePing/Introduction/Intro.html
sdolan