Opening HTML source code in Cocoa - html

I'm trying to display HTML source code in my NSDocument based application. However, it renders the page as Safari would show it.
Here's the code that I use to open HTML:
NSData*data;
NSMutableDictionary *dict = [NSDictionary dictionaryWithObject:NSHTMLTextDocumentType
forKey:NSDocumentTypeDocumentOption];
data = [NSData dataWithContentsOfFile:[self fileName]];
mString = [[NSAttributedString alloc]
initWithData:data options:dict documentAttributes:NULL
error:outError];
What am I doing wrong?

The correct solution is a mix of your original code and the bogus solution I gave you in my previous answer (which I've deleted). Use NSPlainTextDocumentType as the type, as you were doing originally, but use initWithData:options:documentAttributes:error:, not initWithHTML:options:documentAttributes:.
Alternatively, create a plain NSString holding the source code, and then create an attributed string with that plain string plus whatever attributes you want to apply to the whole document (e.g., fixed-pitch font).

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.

iOS Associate URL with Saved File

My app parses an xml, and builds its own custom HTML from the contents of the article chosen in the XML. When I save an article, I have a class for the action, in which I pass the article title, and the custom HTML to strings within the Save class. The class takes that and saves it to the app using:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[thetitle stringByAppendingString:#".html"]];
NSError *error = nil;
[thehtmlcontents writeToFile:pdfPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
The issue that I have is that if I want to share a saved article via Facebook or Twitter, I can't, because the URL doesn't get saved with everything else. I can pass over the URL easy enough to the Save class, but I'm unsure of what to do with it, so that it stays associated with the article itself. Suggestions?
I'd say you broadly have three options:
Attach some metadata to the file noting the URL it corresponds to
Write out a file format that encapsulates the URL, plus the HTML
Include the URL in the HTML in a manner such that you can retrieve it
no. 1 would probably be best achieved by setting an extended attribute on the file. However, I'm not sure how well iOS supports this, and there may well be issues with it not being preserved in the event of something like restoring the OS.
Are you in a position to implement no. 3 reasonably cleanly? I would say a <meta> tag near the top of the document is best for doing this.
All that said, how important really is it that your HTML is stored in files? To me, this sounds like it could easily be chucked into a dirt simple Core Data database.

WebFrame loadData not working?

I'm trying to make a WebView load a page from HTML code I have stored as an NSData. I get a blank page when I try to do this. Is there anything wrong with what I'm doing when I load the page? If not, I need to look elsewhere in my program.
if (essence.html){ //essence.html is an NSData
NSLog(#"Inserting HTML code into browser window: %#", [[NSString alloc] initWithData:essence.html encoding: NSUTF8StringEncoding]);
[webView.mainFrame loadData:essence.html MIMEType: #"text/html" textEncodingName: #"utf-8" baseURL:nil]; //webView is a WebView
}
I created the conditions so essence.html contains HTML code from the page http://kathleenmelian.com/test.html (which just says "hello"). The NSLog prints this when the above code runs:
Inserting HTML code into browser window: <html><head></head><body>hello</body></html>
So essence.html definitely contains valid code that a browser should be able to load.
You could use
[webView loadHTMLString:
[[NSString alloc] initWithData:essence.html encoding:NSUTF8StringEncoding]
baseURL:nil];
The other idea would be to replace "utf-8" with "UTF-8", which in some cases is known to make a difference (not sure about UIWebView).
Sorry to bother you guys. I fixed some other bug in my program's model, and that somehow fixed THIS problem as well. I don't know how. Crisis averted.

Is it possible to make UIWebView load a html string but act like a UILabel

TTStyledTextLabel is exactly what I want, but I don't want to add all Three20 project just for this only one function.
Any suggestions?
If you are OK with limiting the application to iOS 6+, Attributed string support is available on all standard rendering controls, if not, there are many other 3rd party solutions available (including my favorite, OHAttributedLabel). Just don't use a WebView for something this simple, they are really really heavy objects.
Yes, you may use – stringByEvaluatingJavaScriptFromString:
Say you load this string to webView:
<div id="msg">
Super inner html with <b>bold</b>
</div>
Then you may get msg's inner html like this:
NSString* value = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('msg').innerHTML"];
And set msg's inner html like this:
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:#"document.getElementById('msg').innerHTML = '%#'", newValue]];

How can I pull HTML code into an NSString from an iOS app?

I am trying to pull down the code from an HTML website that has no more than 2 lines on it. The code contains a word that I need to retrieve. Is there a simple way to pull down that code and put it in an NSString?
Further details: I am going to have an app that checks for a word on a page. If that word is what I am looking for, the app will show the text "confirmed". The purpose of the app is to check to see if the page is accessible.
If you need a http library to hit the server try asihttp. Apart from this i need more info of what you are trying to do...
If you just want to check if that website is reachable, you can go with HTTP Success Status Codes.
Using ASIHTTPRequest simplifies communication over the web.
If you still want to evaluate the text on that website, can also just retrieve it using:
[request responseString];
Depending on what you get from the website, it's up to you how to update the UI.
Just change the link between the quotes and it'll work!
-(void) viewDidLoad {
NSString * sFeedURL = [NSString stringWithFormat:#"http://www.google.com/ig/api?weather=,,,270000,960000"];
//RSS Feed URL goes between quotes
NSString * sActualFeed = [NSString stringWithContentsOfURL:[NSURL URLWithString:sFeedURL] encoding:1 error:nil];
NSLog(#"%#", sActualFeed);
}