URL with NSString or NSURLConnection

Discussion in 'iOS Programming' started by tdog09, Oct 1, 2008.

  1. tdog09 macrumors member

    Joined:
    Jul 13, 2008
    #1
    Hi all and thanks in advance for any help you can send my way with this!

    I am new to Objective-C (like a lot), but am not new to OO programming (i know java and .net best). I am trying to figure out which is the best way to read a URL page, so I can sift through it for specific "content", and output it in a nice format.

    I've been reading through a lot of the api's, but cannot figure out which is the best approach. Let's say I want to download http://www.google.com. Should I use:

    Code:
    NSString *myUrl = @"http://www.google.com";
    NSString *returnData = [NSString stringWithContentsOfURL:[NSURL URLWithString: myUrl]];
    
    or

    Code:
    NSString *myUrl = @"http://www.google.com";
    NSURLRequest *myRequest = [ [NSURLRequest alloc] initWithURL: [NSURL URLWithString:myUrl] ];
    NSString *returnData = [ NSURLConnection sendSynchronousRequest:myRequest returningResponse: nil error: nil ];
    
    I also will need to eventually post data to a site's form, and I do believe you can do that with NSURLConnection by setting it's header info (i think), but I'm not to that point just yet. Can someone please state the use of NSURLConnection? Thanks!
     
  2. voidptr macrumors regular

    Joined:
    Jan 11, 2007
    #2
    NSString +stringWithContentsOfURL: only works when the URL is a local file. For a network URL, you have to use NSURLConnection. However, you really should use the asynchronous mechanism so you don't tie up your run loop for a lengthy network operation. Also, either synchronous or asynchronous use of NSURLConnection / NSURLRequest return an NSData, not an NSString. To get a string, you'll need to use the appropriate initializer on NSString with the right encoding.

    POST requests use an NSMutableURLRequest with -setHTTPMethod:mad:"POST" and -setHTTPBody:.

    Apple has a pretty good reference guide on the URL loading system you need to read: http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html
     
  3. tdog09 thread starter macrumors member

    Joined:
    Jul 13, 2008
    #3
    ok, so i've been messing around with NSURLCONNECTION for a while now, and still cannot find a way to get a url that requires authentication. I'm using a NSMutableURLRequest object, but have not been able to figure out how to set the username and password.

    For example...

    Say I want to go to a specific url on espn, or sportsline.com, but that page requires me to be logged in, i.e username and password. How do I submit this info along with the url to access the appropriate page?

    Any examples would be a HUGE help! THanks!
     
  4. sujithkrishnan macrumors 6502

    Joined:
    May 9, 2008
    Location:
    Bangalore
    #4

    I found that API reference of NSHTTPURLResponse is in "Core Library" not in "iPhone OS Library". Does this mean that iPhoneOS wont support this?

    But i am making use of this and working fine.. i am confused....
     
  5. walty macrumors member

    Joined:
    Jul 15, 2008
    #5
    hm.. the "NSHTTPURLResponse" does show in my iPhone OS Library documentation.
     
  6. DavidReeves macrumors newbie

    Joined:
    Dec 4, 2008
    #6
    NSURLConnection


    Not sure about development on the Mac, but for the iPhone, this isn't correct. NSString stringWithContentsOfURL works just fine for local and network resources.
     

Share This Page