How to read .html file to Xcode UI - ios

I'm doing the task of changing the rtf files to html files. However,how to read .html files to NSTextView or other Object.I test the following API works well in reading HTML file.
NSTextView *showHtml;
[showHtml readRTFDFromFile:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]];
The object showHtml can shows Html files as the browse does.
But The API readRTFDFromFile descript like that only read rtf or rtfd files.
Are there any API or method to shows Html file in xib.
I also test the API:
[[NSAttributedString alloc] initWithHTML:(NSData *) data documentAttributes:(NSDictionary **) docAttributes],
setAttributedStringValue:(NSAttributedString *) aAttributeString
But It can't shows the attribution except the string .
Thanks for your reply.

Related

Render Images in document.directory in a markdown file in swift(SwiftyMarkdown)

Am using SwiftyMarkdown framework to render my .md file to have dynamic formatted data in my VC. But i am not able to load images in my document directory referenced in .md file. I can load images if they are available in Bundle.
Any other option or framework to fix this issue? I want to dynamically load images from server and save them documents directory and refer them in .md file which is also saved in same documents directory/folder. So the url of image and .md file is same.
"MyFile.md" in documents directory contains below content
Continue your walk, joining up with your original perimeter path of the item. "
In my VC viewdidload, i access the url path and load nsattributed string to my textview using SwiftyMarkdown frmaework
let url = self.getDocumentsDirectory().appendingPathComponent("MyFile.md")
if let md = SwiftyMarkdown(url: url) {
textView.attributedText = md.attributedString()
}
The formatted text is loaded but no image as its not available in my Bundle. But its saved in the same document directory as that of "MyFile.md"
Hope its clear!

Open .odp, .odt, .ods files using WebView

I'm using an UIWebView for viewing .pdf, .txt, .xls, .ppt files. I used webView loadRequest method for viewing these files and its working fine. I need to view files such as .odp, .odt, .ods etc, but its showing only blank page in UIWebView. Is there any effective method for viewing these files using UIWebView or any direct method for doing the same?
Have you tried using the UIWebView method load, specifying the MIME type? For example for an .odt file:
webView.load(loadedData as Data, mimeType: "application/vnd.oasis.opendocument.text", textEncodingName: "utf-8", baseURL: NSURL() as URL)
If that does not work, then I'm afraid you'll have to write your own open office document loader...
WebODF libs integration with UIWebView: Integrated WebODF libs with UIWebView and it's working fine. Please find the below steps
1) Load index.html file from WebODF libs on UIWebView
2) Inject .odt file On webViewDidFinishLoad using below code snippet
let jsFunction = "createEditor(\"../default.odt\", \"Test\", 375, 667)"
webView.stringByEvaluatingJavaScript(from: jsFunction)
3) Successfully load the odt file.
More information : https://github.com/kogmbh/WebODF/issues/945

How to refer a local image file from UIWebView's HTML on iPhone?

I would like to use <img src="temp.jpg" /> in my UIWebView's HTML.
How can I refer to a local file named temp.jpg to save precious loading time?
Of course the obvious choice would be <img src=".\temp.jpg" /> but I have no idea where my root is...
This will create a URL object that contains the location of your file on disk. You can then use this to output a valid file:// URL string that can be used within img tags.
NSURL* fileURL = [[NSBundle mainBundle] URLForResource:#"temp" withExtension:#"jpg"];
NSLog(#"<img src=\"%#\" />", fileURL.absoluteString);
NSURL *url=[[NSBundle mainBundle] bundleURL];
[webView loadHTMLString:string baseURL:url];
This let's the root be the app's main bundle.
I was searching a lot for a solution and found the following which
in my case working fine, I have a local HTML string and a local image
I convert the image into base 64
you may use this website to convert image to base64 encoding
Converter
and rewrite the HTML string to have the base64 image in the image tag like the following
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAABmgAwAEAAAAAQAAABkAAAAA/+0AOFBob3Rvc2hvcCAzLjAAOEJJTQQEAAAAAAAAOEJJTQQlAAAAAAAQ1B2M2Y8AsgTpgAmY7PhCfv/AABEIABkAGQMBEQACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2wBDAAkGBxISEhISEhAQFRUXFRUVEhIQDxAQEBUVFRIWFxUVExUYHSggGBolGxUVITEhJSorLi4uFx8zODMtNygtLiv/2wBDAQoKCg4NDhcQEBctHR0dLS0tLS0rLS0rLS0tLTctLS0tLS4tLS0tLS0tLS0uKy0tLS0tLS0vLSstLS0tLS0rLS3/3QAEAAT/2gAMAwEAAhEDEQA/APTbvxzYRSPE85DoxR18qU4YYyMgc9R0rN1YJ2bO2ll2IqxUoRun5ohPxBsO0sh/3bec/wDstQ8TT7/gdCyXGP7K/wDAo/5iR+PrRnjjQTku6ICYSiguwUElscc0LERbsipZLiYxlKVkkm977a9Dq63PIP/Q6bWbLSftc/nzxLNvLOjXZjJyqnlQeDjHHoRWUqMJO7O+jmWIoxUYSVl5JlOW50RHQLNYsn/LQteM5Xn0zWboJSVo3XU0eb4t/wDLy3okizaeJ9CjlRYRCzl0VWitmcBmkVVzIRgckd62jShHZHPUzDE1FadWTXa56dVnIf/R6/xH8J7W9upbqS4ulaRlYpE0SoCsapwSpPRB3oASD4Pacv3nvG+t0y/+ggUAalh8NNMiIYWxYgggzTTS8g5BwzYzkCgDrsUAf//S9xoAKACgAoA//9k=" alt="Red dot" >
for Testing the result tye the following Online HTML editor
W3School
hope it will help for others or maybe get a better solution, best of luck
I have no idea where my root is...
UIWebView looks up files relative to the HTML file you loaded in it. So perhaps it will be your app bundle.

Image URL from an XML

Hi i am parsing an Xml and gets the data in NSSTring from the description tag as follows.
NSString *Descree= [eleme.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSLog(#"Here- %#",Descree);
i find the following result in console.
<span style="float:left"><img src="http://www.thtfjhj.net/contents/albumsth/57.jpg" /></span><p ><b></b></p>
From this I need to extract the image URL alone.
http://www.thtfjhj.net/contents/albumsth/57.jpg
Your xml contains html content within.
So it would be better if u use html parsing, to extract the required fields.
Here is a great tutorial to do it.
If the html contains multiple img tags, you can fetch each contents using html parsing.
plz use NSXMLParser to parse xml data

iphone image caching from initWithString in webview

i am parsing json from SBJson parser. I am using SDWebIamge for image caching. My JSON data has one tag namely "content" which has text + image links in between text. Just for example something like this
<p>This is sample text.<\/p>\n<p><a href=\"http:\/\/xyz.com\/08\/15.jpg\"><img class=\"aligncenter size-full wp-image-1273\" title=\"15\" src=\"http:\/\/xyz.com\/08\/15.jpg\" alt=\"\" width=\"422\" height=\"720\" \/><\/a><\/p>\n<p><a href=\"http:\/\/xyz.com\/08\/16.jpg\"><img class=\"aligncenter size-full wp-image-1274\" title=\"16\" src=\"http:\/\/xyz.com\/08\/16.jpg\" alt=\"\" width=\"680\" height=\"1024\" \/><\/a><\/p>\n<p> <\/p>\n
which i parse and store in my NSString variable.Then i have a webview which i load from this NSString by
NSString *htmlString = [[NSString alloc] initWithString:myObj.Content];
which loads the text and fetches link images as well by default as we all know web view does.I am saving this JSON data to file for offline viewing which is working great thanks to SDWebimage.But my issue is when i load the save content to webview it shows only text no image cause these is no image cashing in the NSString links which i loaded.So how can i cache the images in my webview so that next time when i load my webview from string it will show images from the link in my NSString data.
Thanks
You can use an existing implementation to do this - something like ASIWebPageRequest or ProxyingUIWebView should to the trick.
Alternatively you could try to implement this yourself. One option is something like the following:
Parse the HTML string and detect any img tags and their src attributes
Download and store any images detected
Display the stored (downloaded images) in place of the online originals by modifying the path to the images in the img tags in your HTML string.
Note this is a far from optimal solution and is only an example of one possible implementation.

Resources