Why is UIWebView canGoBack=NO in iOS7?

后端 未结 4 592
[愿得一人]
[愿得一人] 2020-12-05 15:46

I\'m embedding this web site into my app like this:

NSString *url = [NSString stringWithFormat:@\"https://mobile.twitter.com/search?q=%@\", @\"@test OR #tes         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-05 16:14

    I had the same issue. I am able to resolve it with the following changes.

    Implemented a new method updateButtons

    - (void)updateButtons:(UIWebView*)theWebView {
        if ([theWebView canGoBack])
        {
            self.backButton.enabled = YES;
        }
        else
        {
            self.backButton.enabled = NO;
        }
        if ([theWebView canGoForward])
        {
            self.forwardButton.enabled = YES;
        }
        else
        {
            self.forwardButton.enabled = NO;
        }
         }
    

    Added Calling the above method in shouldStartLoadWithRequest, webViewDidFinishLoad, didFailLoadWithError events.

    Now the tricky part comes. After making above changes, back and forward buttons are working as expected except in one scenario. When we are back to first page by hitting back button, it is not getting disabled. Because it will not fire any of the above events when the page is getting loaded by hitting back/forward button. it just loads from cache.

    I have tried many approaches but only one that solved my problem.

    Added an observer on WebHistoryItemChangedNotification.

       [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(webViewHistoryDidChange:)
                                                 name:@"WebHistoryItemChangedNotification"
                                               object:nil];
    

    Called the same updatebuttons method in webViewHistoryDidChange.

    - (void)webViewHistoryDidChange
    {
         [self updateButtons:self.webView];
    }
    

提交回复
热议问题