NSURLConnection or NSurl?
what is the difference between NSURLConnection
and NSURL
?
i mean if i am downloading a file, does it make and difference wh开发者_如何学Cich one i use?
Rgds
for:
NSString *myUrl = @"http://www.test.com/";
NSString *returnData = [NSString stringWithContentsOfURL:[NSURL URLWithString: myUrl]];
or
NSString *myUrl = @"http://www.test.com/";
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString:myUrl] ];
NSString *returnData = [NSURLConnection sendSynchronousRequest:myRequest returningResponse: nil error: nil ];
whats the difference?
thks
The Connection
An
NSURLConnection
object provides support to perform the loading of a URL request.The Request
NSURLRequest
objects represent a URL load request in a manner independent of protocol and URL scheme.E.g.
requestWithURL
:Creates and returns a URL request for a specified URL with default cache policy and timeout value.
+ (id)requestWithURL:(NSURL *)theURL
The URL
The
NSURL
class provides a way to manipulate URLs and the resources they reference.NSURL
objects understand URLs as specified in RFCs 1808, 1738, and 2732. ...To get the contents of a URL,
NSString
providesstringWithContentsOfURL:
andNSData
providesdataWithContentsOfURL:
.
References:
- NSURLConnection_Class/Reference/Reference.html
- NSURL_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURL
Best thing about NSURLConnection is its asynchronous behaviour so that you dont have to wait until the url is loaded.
精彩评论