UIWebView best way to load an XML - iphone

I want to display Xml file on UIWebView, what i am doing is this:
[m_WebView loadData:[NSData dataWithContentsOfURL:theURL] MIMEType:nil textEncodingName:nil baseURL:nil];
But this is not displaying it properly and i have no control over the color components of it, any suggestions!

You need to use your own style sheet to enable control on component, else web view loads xml as its default format.
Check out following tutorial on css with UIWebView - http://mentormate.com/blog/iphone-uiwebview-class-local-css-javascript-resources/, http://iphoneincubator.com/blog/windows-views/uiwebview-revisited

Related

UIWebView - scaling multipage PDF ToFit

I have a multipage pdf (~14 pages) that I want to load into a UIWebView.
Trouble is that when I activate scalesPageToFit
- The first page is zoomed to ~50%
- The page is centered
- I cannot manually move/scroll the page closer to the page-boundaries
Am using Interface builder.
Any ideas how I can solve this mystery?
Thanks
Try Apple's QuickLook for better PDF display.
Check out this example code: https://github.com/steipete/PSPDFKit-Demo/blob/master/Examples/PSPDFKitExample/PSPDFQuickLookViewController.m
(It's part of http://PSPDFKit.com)

text and image as one scrollable entity

I need an undefined amount of text and one or more pictures to be scrollable as one entity. I'm quite surprised that this doesn't seem to be provided by default, I thought I've seen that several times before... I tried to google, but all I find doesn't fit. The images won't be wider than the screen, but in between lines of text.
I need something that let's me do something like:
image
textA
textA goes on
__ screen ends here, content goes on
textA goes on
textA goes on
image
image
textB
textB goes on
image
textC
The content for the text would come out of a plist, but I THINK I can predict it will be REALLY static, so I could just set the Text in IB and create a view for every content -.-.
I've read about Web View, but as far as I got it, you'd need internet connection to make that work, and the app should work without any internet connection at all.
Any suggestions or experiences concerning that?
Thanks a lot!
There different way to get things done:
If you have static content and want a complicated layout and know how to do it in html you should go with UIWebview and a bundled html file and images and load it with something like:[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"MyStuff" ofType:#"html"]]]];
You can also do a layout with UILabels and UIImageviews and arrange all of this onto one UIScrollview
If it's more dynamic you should go with the latter, but you need to program sort of layout algorithms that handle different number/sizes of images, number/length of test paragraphs and so on.
One option, as you mention, is UIWebView.
It does not require a connection, since you can load a static HTML into it executing:
– loadHTMLString:baseURL:
By specifying a baseURL that "points" to your bundle, you can also include images as resources in your Xcode project and have them displayed (by using <img src=... /img in your HTML):
NSString* basePath = [[NSBundle mainBundle] bundlePath];
[_label loadHTMLString:text baseURL:[NSURL fileURLWithPath:basePath]];
You don't need an internet connection to make a web view work. Look at this method on UIWebView:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

UIWebView Editing the HTML content

I am loading an HTML in my UIWebView. Now I want a user to allow editing this HTML if he wants. Basically it is an email functionality and my body is in HTML format so I render it a UIWebView which works fine. Now if a user wants to reply to this mail, I would like to give him the ability to edit the HTML formatted body. I read something about HTML 5 content editable property but that doesn't seem to help. How can I accomplish it using UIWebView? I have seen the native iphone mail client implementing this functionality.
If there is no other way and I am forced to use MFMailComposeViewController, can I use MFMailCompose View controller to send mails to my own server or for that matter any server?
You can edit the format of the document in a UIWebView through Javascript using the method:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
For example to change the body background do:
[_webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body').style.backgroundColor='#FFFFFF'"];
Using MFMailComposeViewController should be the easiest solution to your problem. As for the To: address, simply set the view controller's recipients using - (void)setToRecipients:(NSArray*)toRecipients.

Programmatically Generate PDF from HTML on iPhone

I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PDF and send it to the user (via email). I am having difficulty with the PDF generation portion.
I have the code to create a PDF using CGPDFContextCreateWithURL but I am struggling with drawing the page using quartz.
I have searched extensively on SO as well as the internet to no avail.
Any help is much appreciated!
To generate a pdf from an HTML, you need to render the html into a web view, and take snapshots of the web view, and render them into an image context.
The tutorial might be helpful:
http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/
I've written a little piece of code that takes an NSAttributedString from DTCoreText, and renders it into a paged PDF file. You can find it on my GitHub Repository. It won't render images or complex html, but it should serve for most uses. Plus, if you're familiar with CoreText, you can extend my PDF frame setter to generate these items.
So what it does now: Give it an HTML string, and it will use DTCoreText to generate an NSAttributedString, then render that into a PDF. It hands back the location that it saved the PDF file in the app's Documents folder.
Why not use a WebService, send the HTML page to this and retrieve the PDF-file ?
That way you can use iTextSharp and C#, and you're done in about 2 minutes.
Plus (if you're evil) you can store and see all the data on your server.
I haven't tried this myself so i have nothing to offer concrete but I'd have to imagine there has to be an easy way to do this on iPhone due to the imaging model. I'd look deeper into the documentation.
As to pushing back with the client that is up to you but there are probably multiple reasons for wanting to keep everything local. Frankly I would not be pleased at all to here from somebody I hired that he couldn't manage this particular task. So think long and hard about this push back. Oh even if you do push back a webserver is a poor choice. I'd go back a step further and investgate why you need something in HTML in the first place.
I've never tried this so I have no idea if it'll work, but how about loading the HTML into a UIWebView, and then make the view draw itself into a PDF context? E.g.
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
[webview loadHTMLString:html baseURL:...];
Then:
- (void)webViewDidFinishLoad:(UIWebView *)webview {
CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
[webview.layer drawInContext:pdfContext];
...
}
I made it by following this SO: https://stackoverflow.com/a/13342906/448717
In order to maintain the same content's proportions I had to multiply the size of the WKWebView 1.25 times the printableRect's size set for the UIPrinterRenderer, as the screen points differs from the PostScript's... I guess.

Updating UIWebView off screen?

I'm trying to get a UIImage of a UIWebView, I can do this fine using renderInContext:. However before I generate the image I would like to update the UIWebView with some HTML code, the trick is the UIWebView is hidden. Right now, I can not get the updated version of the web view to render in the UIImage, just the old one.
Are you waiting until the UIWebView re-renders before querying it again? Loading HTML into a UIWebView does not immediately update the view. You need to wait for webViewDidFinishLoad:. This is true even if the HTML is a simple string with no external references.