UIWebView: Saving web page

Discussion in 'iOS Development' started by johnmabassa, Apr 9, 2009.

  1. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    Hi,
    I have an application which launches a site in UIWebView. How can I save that webpage so that it can be viewed later?.....

    I have seen some applications like Instapaper and NYTimes which stores the pages which can be saved and read online, I wanted to do the same stuff but not sure how to do it.....

    -John
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I've never had to do this, but I'm sure you can just do something such as:
    Code:
    [yourWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
    to get the html, then just write that to file as normal. You can either create an NSURLRequest out of that to load into your webview to display it, or simply feed it directly into your webView with
    Code:
    [yourWebView loadHTMLString:theStringOfHTMLYouObtained baseURL:nil];
    Although, admittedly, my javascript is super rusty, so you may need to tweak what I wrote to make it work.

    Also, don't forget to get all the supporting files (css/js files/etc) if necessary, and to cache all the images, else this won't be worth it.
  3. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    I will give it a try......


    Thanks for the reply
    -John

    PS: I was trying to replicate the NYTimes application ( just for learning purpose) there they are allowing the user to save the article they are viewing, to "saved folder" and user can read it whenever he wants (online/offline)...... it is my guess that they are using "UIWebView".
  4. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    Almost done it

    Hi

    I tried to do this

    NSString *str=[myWebView stringByEvaluatingJavaScriptFromString

    Please Register or Log in to view images

    "document.getElementsByTagName('html')[0].innerHTML"];

    I didnt get anything in str ( I am also not familiar with java scripts and not sure what the above script returns

    Please Register or Log in to view images

    )

    I found another way to do it, here is what i done

    1. Loaded the web page in webView.
    2. After webViewDidfinishload, I stored the contents into an NSData object and then converted data to an NSString and wrote to a file and saved in the application folder.
    3. Now while trying load it, I am taking the string back from the file and converting it back to NSData and loading it to webView using loadData: method.

    I am able to get the saved webpage again in the webView (even offline).

    Now I have two problems left

    1. The web pages I am trying to save are simple (doesnt have much css and all) and the problem I am facing is that I am not able to load images, since most of the images in an html are external links, it requires net connection to load it. Any idea how to get images too to get saved to my file?

    2. The second problem I am facing is how to delete a saved file from app's folder. I am able to get the list of files in the application's folder and get it populated in a table view and now I want to delete the file when i do "swipe to delete"


    PS:- I am not sure whether I am doing things right, but its meeting my needs.

    Thanks in advance
    -John
  5. mdschm2

    mdschm2 New Member

    Joined:
    Aug 5, 2008
    Messages:
    1
    Likes Received:
    0
    Device:
    2G iPod touch
    Updates

    Hey John, I've been trying to do what you've accomplished for some time now. Do you think you could post some detailed code or a tutorial outlining how you saved the views and reloaded them offline?

    Thanks!

Share This Page