views:

2287

answers:

2

I have the following code.

NSData *pageData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *webpage = [[NSString alloc] initWithData:pageData encoding:NSUTF8StringEncoding];

This works fine with most pages but truncates the really long ones, is there a way around this at all?

+3  A: 

Check out this method:

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithContentsOfURL:encoding:error:

If you're just using a synchronous request, this should be fine.

Dan Lorenc
Hi Dan, What section is applicable, I have searched up and down a few times.
Lee Armstrong
The section I linked to: stringWithContentsOfURL: encoding: errorThat method will automatically download the url and place the result in a string.
Dan Lorenc
+2  A: 

I believe this is what you are looking for (from the NSString class reference linked above):

stringWithContentsOfURL:encoding:error: Returns a string created by reading data from a given URL interpreted using a given encoding.

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

Parameters url The URL to read.

enc The encoding of the data at url.

error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, you may pass in NULL.

Return Value A string created by reading data from URL using the encoding, enc. If the URL can’t be opened or there is an encoding error, returns nil.

nicktmro