UIWebView won't goBack after loading HTML in i

2019-05-02 02:59发布

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?

3条回答
对你真心纯属浪费
2楼-- · 2019-05-02 03:20

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];
查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-02 03:39

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];
}
查看更多
Viruses.
4楼-- · 2019-05-02 03:41

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.

查看更多
登录 后发表回答