How to create DOM model in iOS SDK - html

How to create DOM model in iOS SDK to update changes in HTML file? I am making a project so where I have HTML file to convert PDF format so first I create HMTL file and then programmatically update dynamic data in HMTL file.
So how do I use DOM model in Objective-C to change HTML file data?

First create your HTML File with different tags as like "##v1#" there you want to edit your text and value. and call below function in your code.
NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"page02" ofType:#"html" inDirectory:NO]encoding:NSUTF8StringEncoding error:nil];
html = [html stringByReplacingOccurrencesOfString:#"##v1#" withString:completeHTML];
[self.webViewTest loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
I think it would be help full for you.

Related

How do I display a jpeg image stored in a core data database using html

I have several images soared in a core data database in this way. The entity is named note.
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
image = nil; // free memory
[self createNote];
note.photo_jpeg = imageData;
How do I reference the images in html generated for a web page to display several of these images? I think I need something like this, but I don't know what to put in the IMG SRC=...
NSString *imageHtml = [NSString stringWithFormat:#"<IMG SRC="what do i put here!!!" ALT="Photo" WIDTH=%i HEIGHT=%i>", , )];
[html appendString:imageHtml];
Update This is the solution I used:
[html appendFormat:#"<img alt=\"Embedded Image\" src=\"data:image/jpg;base64,%#\" WIDTH=400 />", [currentNote.photo_jpeg base64EncodedStringWithOptions:0]];
Where currentNote is of type note and is indexed through the notes I am displaying.
You'll need to put the image data inline with the HTML, encoded as base64.
Something like this:
NSData *imageData = // from your code
NSMutableString *html = // mutable string with whatever else you need
[html appendFormat:#"<img alt=\"Embedded Image\" src=\"data:image/jpg;base64,%#\" />", [imageData base64EncodedStringWithOptions:0]];
Keep in mind that this duplicates the image data, so if you're using a lot of images this way, make sure to watch how much memory you're using.
NSData have required methode to get base64 string.
img src="data:image/jpg;base64,HereBase64RepresentationOfYourJPG"
I do not think there is a way to have HTML dip into core data.
I would create a sub-directory for your core data store. In that subdirectory, create a sibling directory to hold your images.
You can store the images as plain jpeg files, and keep the path or bookmark-url to the file in your core data object.
This way, you can still access everything via core data, and the file is available to the HTML as well.
Just include the path to the file as part of the html.

iOS7: Display an Image in an HTML File loaded into a UIWebView

I am programming in iOS7. In my app I am using a UIWebView to load static HTML files that are packaged with the app. I want to show images, also packaged with the app, in my HTML files, but can not find the magic combination to get the images to show when the HTML file is loaded.
I saw some information on base64, which I did not understand, but I would like to find a simple solution because there will be others beside myself writing most of the HTML files.
Below is the basic format of the HTML code I am using. I have tried all manner of “/“ modifiers to no avail, including the full path as listed in the File Inspector.
<img src=“data:/Supporting Files/Info/InfoRecycle.png">
Does anyone know how I get the image to show within the HTML file?
Try putting the image in the same folder as your html file. Then reference it like this:
<img src="InfoRecycle.png">
See if that works.
I finally got it to work! I found the key to the answer in this post. I guess I did not find it earlier because I was focused on iOS7:
Using HTML and Local Images Within UIWebView
I had to make some modifications in my Xcode, so it now looks like this:
//..Loads a Local HTML File
strHTML = #"Info";
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:strHTML ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_infoWebView loadHTMLString:htmlString baseURL:baseURL];
My reference to the image in the HTML code is now as suggested above by Stepan Grigoryan.
<img src="InfoRecycle.png">

How can I overwrite or save an existing local HTML file that's edited in a UIWebView?

The app uses a UIWebView to load up local HTML files (which are now just text) for editing. I need to save whats been edited by the user back to the local file. This is the only way I could think of doing that:
Once the text has been edited by the user, I've written a save method that takes the current HTML in the web view and saves it to an HTML file (with the same fileName as the original) which is written to the documents directory. In theory this should overwrite the original. But it doesn't.
-(void)save{
NSError *err;
NSString * docsDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *oldFile = [self.webView.request.URL lastPathComponent];
//Above line gets the actual HTML file name that's in the webview
NSString * path = [docsDir stringByAppendingPathComponent:oldFile];
//The following line gets what html code is in the actual webview
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('html')[0].innerHTML"];
[html writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
No file seems to be written. It doesn't overwrite the original nor does it write a new one.
Perhaps I shouldn't be saving it this way, but I can't think of any other methods of doing it.

UIWebView load html file from bundle, and provide file with information?

I'm interested in replicating Mobile Safari's "Cannot Open Page" StandardError page.
In order to do that, I've been researching how mobile safari handle's an error when loading a page and opens the html file located in the application bundle.
In that bundle, the file: StandardError.html is loaded and produces an error while showing the mobile safari icon. The one thing that I'm having trouble with is understanding how the error is passed from the webview to the html file.
In the body of the html file, there is a variable with %#. I'm assuming that the %# indicates where Apple provides the error that caused the webview to fail loading. What I want to know is how to provide the title and error to an html file, similar to the StandardError.html file found in Mobile Safari's application bundle.
Any help would be greatly appreciated!
A UIWebView can load HTML from a string so you can do this:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"StandardError" ofType:#"html"];
NSString* errorHTML = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *htmlString = [NSString stringWithFormat:errorHTML, weberror.localizedDescription];
[self.webView loadHTMLString:htmlString baseURL:nil];
Or something like that.

Generating the html file dynamically from the user inputs

I have a requirement for my project . I want to generate html file dynamically from the user
inputs. I have searched a lot , but I couldn't find any links for this.Any help on this ?
I have done similar thing. I think what you need to do is. create a template html page with container divs with id and load that file. On the delegate method of webview onPagefinishedloading run java script to add the information dynamically on the divs to the template html page.
NSBundle *bundle = [NSBundle bundleForClass:[self Class]];
NSString *htmlPath = [bundle pathForResource:#"html file name" ofType:#"htm"];
NSString *htmlContent = [NSString stringWithContentOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
//Do your HTML edits here
[[self webView] loadHTMLString:htmlContent baseURL:nil];