Loading HTML into a Cocoa app with WebView and change text - objective-c

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.

Related

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];

best way to get text from a plain .txt file into a Scroll View

Can someone please help me out with this I'm actually going nuts!
What is the best way to get text from a plain .txt file into a Scroll View, thats all I need just text.
I've tried so many different solutions but can't get any of them working I was hoping someone could give me a fresh overview.
Is the file a resource in your application or are you loading it from a network resource? If embedded, you can load it into an NSString object and then set the text property of the UITextView with that. Something like:
UITextView *myTextView = [[UITextView alloc] init];
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:#"yourTextFile" ofType:#"txt"];
NSString *theText = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
myTextView.text = theText;

Set contents of webview to html string (cocoa)

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.