Is it possible to retrieve website source code for parsing in an iPhone app?
I'm making an application that directly takes images from a site and downloads them into the apps Documents directory for use when the user doesn't have a data connection. The application will check the site for the latest post and update accordingly.
I've never done anything with databases before and thought that this would be easier to implement since I'm only looking for one specific portion of the webpage.
开发者_如何学JAVAAnyone ever do anything like this? Is this even possible?
Are you trying to mirror an entire, arbitrary pages for offline access, or do you just want to pick out specific images on certain page and store those?
Would this be for arbitrary pages, or for sites that are known in advance, so that you can inspect the HTML a little in advance, allowing you to pinpoint where on it your interesting images usually are?
You might want to have a look at hpple, an Objective-C version of hpricot, which can be used to parse HTML coming from web pages. If you know the DOM of the page that your app monitors, you could use it to extract the image names and then download them.
You're going to have to consider what to do if the site you're getting these images from changes its HTML/CSS dramatically. Bake too many specific assumptions into the app and it would need to be updated to work again. Without knowing specifics of what your app is trying to do, I can't tell if this is going to be an issue for you.
Do you control the website component, or is it sucking data from an arbitrary location?
If it's the former, it would make sense to output the data in XML for consumption by the iPhone. If it's the latter, you'd need to read and parse the (X)HTML DOM tree, which would be quite a bit more complex.
Either way, all you're doing is reading the data from the web server responsible.
Update: As a starter for ten, take a look at the URL Loading System Overview docs. (The NSURLConnection is a good starting point.)
精彩评论