views:

62

answers:

1

Hi,

what is the difference between NSURLConnection and NSUrl?

i mean if i am downloading a file, does it make and difference which 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

+1  A: 
  • 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 provides stringWithContentsOfURL: and NSData provides dataWithContentsOfURL:.

References:

The MYYN
it means nsurlconnection is just to load a webpage, whole webpage will be display, nsurl is for getting data within the webpage?
Stefan
No, read the links provided by The MYYN. NSURL is a model class that represents a URL. NSURLConnection is a class than ables you to download data from a URL. In order to use an NSURLConnection, you need to create a URL request using NSURLRequest, which uses NSURL to represent it's URL. Read the Cocoa documentation entitled Cocoa URL Loading System.
Jasarien
for:NSString *myUrl = @"http://www.google.com";NSString *returnData = [NSString stringWithContentsOfURL:[NSURL URLWithString: myUrl]];orNSString *myUrl = @"http://www.google.com";NSURLRequest *myRequest = [ [NSURLRequest alloc] initWithURL: [NSURL URLWithString:myUrl] ];NSString *returnData = [ NSURLConnection sendSynchronousRequest:myRequest returningResponse: nil error: nil ];whats the difference?
Stefan
I would be clearer if you editted your above comment with the code into the question, as it's almost unreadable in a comment. The answer however is that there is very little difference; the second method gives you more scope to handle errors. Both methods are syncronous; you should use asyncronous networking instead so your application does not lock up whilst fetching from the network.
JosephH
my apologies. edited my post
Stefan