I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.
Related
I need to display webpage using URL. From that page I want to remove one link control from that html page. Is it possible? I am opening it with this kind of code.
NSString *urlAddress = #"http://myurl.com";
NSURL *url = [NSURL URLWithString:urlAddress];
If you want to disable ALL links, see the answer from user3182143...
If you only want to disable a specific link, you can still use those methods - you will just need to evaluate the request part of shouldStartLoadWithRequest:(NSURLRequest *)request and decide whether to return YES or NO
If you want a specific link to look different, you'll need to modify the html. Couple ways to do so, but we really need more information about exactly what you are trying to do.
Are you displaying ONE specific web page? And all you need to do is change ONE link in that ONE page, and you're done? Are you displaying a series of pages? Do you have any control of the pages? And so on.
I'd suggest you start searching for and reading about uiwebview insert html or uiwebview insert javascript
We can do this.
First we should know about the UIWebViewDelegate methods
First create BOOL
ViewController.h
#property (nonatomic) BOOL ret;
ViewController.m
#synthesize ret;
Set BOOL to NO in below delagte method
- (void)webViewDidFinishLoad:(UIWebView*)webView {
// Sent after a web view finishes loading a frame.
ret = NO;
}
Then If we return NO in below delegate method, this will prevent the user from viewing link
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return ret:
}
Dave DeLong's Disable Link in UIWebView answer
Removing hyper links from a URL shown in UIWebView
Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.
First thanks in advance to have a look on my issue. My issue is my am developing webview based application. It's same like as browser but i have a issue related to reload web view. When i am getting a value form popover bookmarked and try to reload the current web view from this next bookmark url, my web view do noting. I am stuck in this issue. Although when i enter any url through it works perfectly. May i doing something wrong with web view ?
temp = appDeleg.strURLFromPopover;
NSURL *url = [NSURL URLWithString:temp];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:requestObj];
[self.myWebView setDelegate:self];
[self.view addSubview:myWebView];
Please have a look..
The last line looks like very suspicious. In my experience, [self.view addSubview:myWebView] should be placed in the "initialization block" to avoid some lazy init issues. Are you sure All objects are NOT nil? if so, you can try to set UIWebView's delegate to your class, and debug in each method to test if your object are properly initialized. I tried your code, work fine in my side. Below are delegate method you might consider about
– webView:shouldStartLoadWithRequest:navigationType:
– webViewDidStartLoad:
– webViewDidFinishLoad:
– webView:didFailLoadWithError:
i have a string called htmlString that contains some informations formatted in html. I need to put these info into a webView that load the entire html string, with color and fonts. And i need to know the string height. How can i do?
You want to do something like:
[_webView loadHTMLString:htmlStr
baseURL:[NSURL fileURLWithPath:path]];
You can view the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
This will load your HTML into the webview and use the path you provide as a root for other documents. In other words, your html string could reference other files (css, javascript, etc...) and the baseURL is used to locate the urls that use relative paths.
EDIT:
To get the height, you could assign the UIWebView's delegate as it has a webViewDidFinishLoad: method to tell you when the page is rendered. Then you could execute javascript on the page to determine the final height using UIWebView's method - stringByEvaluatingJavaScriptFromString:
This answer also seems pretty relevant: How to determine UIWebView height based on content, within a variable height UITableView?
NSString *htmlStrings="Hello I m here";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];
I'm coding up my firs iPhone app..
How can I use "loadHTMLString:baseURL:" to send the user to a view called "pagetwo.m"?
To navigate from one view to another or to insert subview given solution is correct and following is the sample code to let you know, how to use delegate method of UIWebView loadHTMLString:baseURL:...
NSString *embedHTML = [NSString stringWithFormat:#"<HTML><HEAD><TITLE>Page 1</TITLE></HEAD><BODY><H2>HTML Code Editor</H2><H3>p1 - The Basic Page</H3>"
"<P>This is the basic webpage. The HTML tags, BODY, H2, H4, P and A were used, but since no attributes were added to these tags, they are left at their defaults. The links below are shown using their default colors.</P>"
"Google.com<BR>Yahoo<BR></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];
I assume pagetwo.m is a UIViewController subclass, right? You can't send a user to view with loadHTMLString:baseURL: since that's a UIWebView's method to load html page into webview. You will need something like:
[self presentModalViewController:pagetwo animated:YES];
Or if it's a subclass of UIView, you need:
[self.view addSubview:pagetwo];
But before that you need to alloc/init pagetwo (and release it later) which will be hard to do since your class is also named "pagetwo" and you instance variable can't be called the same. you could call it (the instance variable) pagetwoview or something, but preferred way would be to follow Objective-C naming conventions and always name your classes starting with a capital letter.