Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tdog09

macrumors member
Original poster
Jul 13, 2008
73
0
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!
 
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!

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
 
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!
 
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


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....
 
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....

hm.. the "NSHTTPURLResponse" does show in my iPhone OS Library documentation.
 
NSURLConnection

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


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.