Save webview to xml stored in application?

Discussion in 'iOS Programming' started by newtoiphonesdk, Jul 3, 2011.

  1. newtoiphonesdk, Jul 3, 2011
    Last edited: Jul 4, 2011

    newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    How do you save the text from a webview into an xml in the application documents directory? I want to do this so I can let the user save an article for a blog app in the iPhone to read even when the iPhone has no network signal.

    Here is the code I have for the WebView that loads the original article.
    Code:
    -(IBAction)save {
    		NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_entry.articleUrl]];
    	NSString *applicationDocumentsDir = 
    	[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    	NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"sample.xml"];
    	
    	// write to file atomically (using temp file)
    	[data writeToFile:storePath atomically:TRUE];
    }
    
    Here is the viewDidLoad I have for the controller I want to load the articles:
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];    
    	[activity startAnimating];
        self.title = @"Saved Articles!";
        self.allEntries = [NSMutableArray array];
        self.queue = [[[NSOperationQueue alloc] init] autorelease];
        self.feeds = [NSArray arrayWithObjects:@"sample.xml",
                      nil];    
        [self refresh];
    }
    
    Does this look like it is anywhere close to where it should be?

    Ok, I realize a few things about why this will not work, but not 100% sure how to proceed. I was able to open the applications document directory and view the sample.xml file it created, which had errors when tried to open. I realize that this method won't work because it is just writing the URL to the xml and then trying to parse it again, so I would still need internet. What would be the best way to go about doing this right?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    If you're loading the data into UIWebView first, you may be able to use stringByEvaluatingJavaScriptFromString: and some JavaScript to get the HTML and then write that to disk.

    Or you could load it with NSURLConnection and pass it off to the UIWebView, but that won't get other things like images, CSS, etc (neither will saving it).
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    If I can use the javascript method to get the inner body text and title of the article, how would I write both of those to an xml, so that when I go to the controller with that xml parsed, the title shows up in table view and text shows up in detail view?
     

Share This Page