What is the fastest way to get the contents of a url string using Cocoa / iPhoneSDK?

Say I want to get HTML

http://www.google.com

as a string using some of the built-in Cocoa Touch framework classes.

What is the minimum amount of code I need to write?

I got this far, but I can’t figure out how to move forward. There should be an easier way.

CFHTTPMessageRef req;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
req = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
                                 CFSTR("GET"),
                                 (CFURLRef)url,
                                 kCFHTTPVersion1_1);
+3
source share
2 answers

The fastest way is to use the NSString method +stringWithContentsOfURL:. However, this is a modal call, and your application will be inactive during its launch. You can either move it to a background thread or use the NSURLConnection class to make the correct asynchronous request.

+9

, , Ben Gottlieb, synchronRequest , , .

NSURL *url = [ NSURL URLWithString: @"http://www.google.com"]; 
NSURLRequest *req = [ NSURLRequest requestWithURL:url
                                      cachePolicy:NSURLRequestReloadIgnoringCacheData
                                  timeoutInterval:30.0 ];
NSError *err;
NSURLResponse *res;
NSData *d = [ NSURLConnection sendSynchronousRequest:req
                                   returningResponse:&res
                                               error:&err ];

dev-docs Apple.

+5

Source: https://habr.com/ru/post/1698779/


All Articles