UIWebView back button not getting enabled - iphone

In my application I have a UIWebView in which I need to add browser like functionality(user can goBack,Forward or reload the page). To achieve this I have used the code for this tutorial.
In my code I have only one change in viewDidLoad:
I am loading data from a local html file like this:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"File name" ofType:#"html" inDirectory:NO];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[self.webView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
instead of:
NSURL* url = [NSURL URLWithString:#"http://iosdeveloperzone.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
After loading initial html file if I am making any click on that page than the back button should get enabled, instead of its getting enabled after second url click and so that I am not able to go back to the original home page.
Please help me with this.
Thanks.

I got an answer. It's because you load data into the webView without loadRequest. If you load data into webView as a string, then it won't have any url to go back or load the old page. So it won't store the data you given to the webView. For that, you need to provide the url as like this.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"File name" ofType:#"html"] isDirectory:NO]]];

Related

iPhone app with local HTML file doesn't support link

I want to create simple iPhone app with local HTML file.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[waWebView loadData:htmlData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#""]];
I made one webView and make it show index.html file in same directory with .m file.
Then make an another HTML file; index2.html, in same directory
I want to see index2.html through a link in index.html
So, in index.html, I put
View html 2
index.html is successfully shown, but above link doesn't work.
What do I need more? Help meT.T
In additional to this problem, this app doesn't support javascript code too...
the link is relativ.. you need to set the baseURL of your webView to the path containing the index html file
NSString *file = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *base = [NSURL fileURLWithPath:file.stringByRemovingLastPathComponent];
[webview loadHTMLString:text baseURL:base];
It is done by creating a NSURL using its fileURLWithPath and then a NSURLRequest and loading that rather loading the html itself:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:htmlFile];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[waWebView loadRequest:request];
You may need to add javascript to it for it to work. If you want to make a simple iPhone app just use app mobi(http://www.appmobi.com/) and they let you put css html and js files into a builder and it does the magic for you. Try that.
Hope I Helped
LucaSpeedStack

UIWebView load HTML file from local file system

How can I load a HTML from the local file system?
I have seen many questions like this but they are all referring to a file that is within a bundle. I would like to load from a path such as/Users/Abs/Documents/ - I think the ios simulator has access to this as I can save files to my local /Users/Abs/Documents/ directory.
But I don't know how to load a HTML file to the UIWebView.
How can I edit the below to achieve this:
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index.html" ofType #"html"]isDirectory:NO]]];
use below code it will work
NSString *filePath=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
[webView loadData:[NSData dataWithContentsOfFile:filePath] MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:nil];
NSURL *url = [NSURL URLWithString:#"/Users/Abs/Documents/index.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[web loadRequest:req];
Note that this is only useful when working in the simulator, because apps on a device are sandboxed and can only get to files in their bundle.

how to show rss feed url in UIWebView in iphone sdk?

I am trying to display url in UIWebView but I can not display, webview just call the fail with error method. My url is this "http://www.tekniknoktamarket.com/index.php?route=feed/google_base",
So please help me how to display this url in UIWebView *?*
In order to have a UIWebView download and display the content from a URL, you need to go through a couple of layers of abstraction, the first being an instance of NSURL, which contains your URL string. Then, you hand the NSURL to an instance of NSURLRequest. Finally, you hand the NSURLRequest to the webView. It all looks something like this:
NSURL *url = [NSURL URLWithString:#"www.your-url.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[yourWebView loadRequest:request];
That should do it.
This is the code loading a URL into a webview:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.theurl.com"]]];
If you're using this code and are encountering any problems, please provide the code you were using and the exact error

Can't load JavaScript from Resource in UIWebView

I am loading a local HTML file into a UIWebView in iOS using this code
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
The page works fine, and the image linked within the HTML loads fine. However, the JavaScript linked off of the page is not loading.
The HTML, Image and JavaScript are all located in the same folder. (Resources/Html in my Project)
How can I get the JavaScript to load?
The problem was that XCode was showing me a warning:
warning: no rule to process file
'$(PROJECT_DIR)/html/orientation.js'
of type sourcecode.javascript for
architecture i386
This answer contains the solution. Basically, I had to tell XCode to not try and build the JavaScript files, and manually drag them to the "Copy Bundle Resources" directory.
The following code works for loading an html file which has javascript files in the web view
Here web is web view object and index is the html page
Web.delegate = self;
[Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];

how to get a web page displayed on my uiwebview in objective -c

I am trying to display a web page in UIWebView so that when i click a button the webpage should be displayed in uiWebview.How could this be possible Can anybody help in solving this or give me some hint
You should have a look at the documentation for UIWebView.
There are two ways of doing this. The method you choose depends on where the data is coming from. If you're loading the data from a string, use:
NSURL* base = [NSURL URLWithString: #"http://..."];
[myWebView loadHTMLString: #"some html here" baseURL: base];
When loading HTML from a string the UIWebView needs to know the base URL for any relative URLs within the document, hence the baseURL: parameter.
If the data is accessible via a URL, use:
NSURL* url = [NSURL URLWithString: #"http://..."];
NSURLRequest* request = [NSURLRequest requestWithURL: url];
[myWebView loadRequest: request];