Set contents of webview to html string (cocoa) - objective-c

Is there any easy way to set the contents of a webview to an HTML string (without having to load a file)?
Thanks

[[webView mainFrame] loadHTMLString:htmlString baseURL:someURL];
(Where someURL is used to resolve relative URLs in the HTML source.)

[webView loadHTMLString:yourString baseURL:[[NSBundle mainBundle] bundleURL]];
You can use [[NSBundle mainBundle] bundleURL] if you desire to load stylesheets(css) or javascripts(js) files that are in your resources dir, if not, just use nil

Yes definitely calling loadHTMLString:baseURL: on the mainFrame of the webView would do it.

Related

How do you set what about:home is on webView, Obj-C

I am creating a Cocoa web browser, and I noticed that if the webview loads a nil location, it just loads about:home. Since I have not set it, the page just appears white. Is there a way I can change what about:home looks like. Even if it is a simple .rtf file or something.
I looked around, but don't see any way to do this. Am I suppose to create a NSURL and set it to whatever file?
Thanks. Oh, and if code is ever needed, I would be glad to add it.
Try something like this:
// Inside your App Delegate
-(void)applicationDidFinishLaunching:(NSNotification *)notification {
// Assuming WebView is called myWebView
NSString *currentURL = [myWebView mainFrameURL];
if(!currentURL) {
NSString *homeResource = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html" inDirectory:#"default"];
NSURL *homeURL = [NSURL fileURLWithPath:homeResource];
NSURLRequest *request = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:request];
}
}
You'll need to have a pre-made file called home.html within a folder called default located in the Resources section of your project.
I suppose this isn't exactly replacing about:home, but you can always check for about:home and handle that appropriately as well.

Loading local HTML into UIWebView on iOS not showing local images or styling

I'm having an issue getting local images and css stylesheet to show through on a local HTML page using UIWebView on an iPad app that will be using iOS 5.
The HTML page itself shows through and is laid out well, which is great. But the problem is it has blank squares where images should be coming through, and styling on text or anywhere isn't showing either.
I've come across very similar questions on SO where people have said to load the HTML into the view with the baseURL, which I then did using an answer from their question. But the images and style still did not come through.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"htm" inDirectory:#"html_files"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[web loadHTMLString:htmlString baseURL:baseURL];
The UIWebView is put in place using Interface Builder and I have that in the .h file as #property(nonatomic, retain) IBOutlet UIWebView *web;
Inside my html_files folder I have:
index.htm
img (folder)
style (folder)
Using the following code, the log showed "index.htm", img, style which tells me it's keeping the directories the same, but I still even tried placing and then linking to images in the root of html_files but still nothing.
NSString *htmlPath = [path stringByAppendingPathComponent:#"html_files"];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:htmlPath];
NSLog(#"%#", dirContents);
And then within index.htm I'm linking to images like <img src="img/logo.jpg">
Any ideas would be much appreciated, thank you.
EDIT: Added link to project here: http://www.filedropper.com/localhtml (it's literally just a UIWebView with the HTML files so nothing big and messy)
Okay, here a re the mistakes you made, i said just use only image name in the img tag, you where using the path.
In iOS not mater how many folders you have, where ever you have the resources, it just adds all the resources in a single bundle.
Here is the link, check out i have made the changes only in index.htm

How to apply css to a native ios app?

I am working on a app were I need to apply the css style sheet to the background as a theme. Is there a possibility to apply programmatically by dragging the css file into resource folder and calling it from the documents folder.
please help.
Thanks.
I'm using a custom CSS style sheet I added to my resources folder to style a UIWebView object. First, I put the HTML code I want to load on an NSString and I specify the style sheet I want to use. Then, I create the UIWebView object and load the HTML string with the method loadHTMLString:baseURL: where the base URL is the path to your app's bundle. Hope this helps.
NSString *bodyHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\" /> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", textPost.body];
UIWebView *bodyView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 400.0, 532.0, 10.0)];
bodyView.delegate = self;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[bodyView loadHTMLString:bodyHTML baseURL:baseURL];
Have you tried using a UIWebView?
It offers a – loadHTMLString:baseURL: method that will allow you to do what you describe.

Get url variable

I have a application on iPad 1 and a PDF with several links.
The client will click on that link and I need to get its variables values and use it on my application.
Is it possible?
Thanks in advance
Use a UIWebView and load the PDF in it. Something like this (note - it's untested but should get you on the right track!):
NSString* basePath = [[NSBundle mainBundle] bundlePath];
NSURL* baseurl = [NSURL fileURLWithPath:basePath];
[self.webView loadData:MIMEType:#"application/pdf" textEncodingName:nil baseURL:baseurl];

Loading HTML into a Cocoa app with WebView and change text

Right, so far I've got this for loading up a HTML file from within my app:
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"html"];
NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[[webView mainFrame] loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
Which works loading the file up into the app's WebView. But I want to change some values in the HTML (or PHP if it works by using $_REQUEST/$_GET), for example, I have a table with some text in it and I want to change this text from an NSString etc.
How would I go about this?
You can execute JavaScript on UIWebView, if you need to do that dynamically.
Or, if you want to refresh the whole page, you can modify string loaded from file according to your preferences.