Parse HTML in Objective-C

This is for the iphone

This is what i am trying to achieve:

This website expands URLs like the creator got an API that says that all you have to do is go to this website:

so when i put:

it returns a new website with this information:

    "status": "OK", 
    "end_url": "http:\/\/", 
    "redirects": 1, 
    "urls": ["http:\/\/\/miniscurl", "http:\/\/"], 
    "start_url": "http:\/\/\/miniscurl"

And that is great! but how do i get that information into an NSString and then search through it for the different tags. so that at last got this:

NSString *status = OK;
NSString *end_url = http:\/\/";

Also a NSArray containing all the redirects (if there are more than one) would have been great!


I need:

  • a fast and easy way to get HTML source from a website.
  • a fast and easy way to search through NSString and cut it up in different pieces.

Thank you!

Best regards Kristian


To get the data from an HTTP-server, you can use [NSData dataWithContentsOfURL:].

To parse the data, in this case it seems to be JSON-data, you can use a simple JSON parser like TouchJSON.

Here is some example code I wrote for one of my apps:

NSURL *url = [NSURL URLWithString:@""];
NSData *rawJsonData = [NSData dataWithContentsOfURL:url];

CJSONDeserializer *parser = [CJSONDeserializer new];
NSError *error;
NSDictionary *jsonDictionary = [parser deserializeAsDictionary:rawJsonData 

[parser release];

Hope that helps!

Also, it looks like you are new here. Please mark the answer that helps you most by clicking the "√" on the left, thanks!

Need Your Help

Reordering controls on UITableView while not in editing mode?

iphone ios4 uitableview order

I have a UITableView and I want to be able to reorder the rows when I'm not in edit mode, meaning I don't want to see the delete icons until I press Edit. I want to be able to see and use the reord...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.