I am loading request like this [resultsWebView loadRequest:searchRequest]; Then I do this
- (void) webViewDidFinishLoad:(UIWebView *)webView {
if ([resultsWebView canGoBack]) {
[goBackButton setEnabled:YES];
}
else {
[goBackButton setEnabled:NO];
}
if ([resultsWebView canGoForward]) {
[goForwardButton setEnabled:YES];
}
else {
[goForwardButton setEnabled:NO];
}
}
canGoBack is always returning NO.
It was working earlier, but it has stopped working suddenly(I have not done any code changes). I don’t know how is this possible? Not getting any success to resolve this. There is a question on stackoverflow UIWebView canGoBack and canGoForward always return NO. But it is different as the question author was using loadData and he resolved the problem using loadRequest. But I am already using loadRequest. And again, it was working earlier, but it has stopped working suddenly(I have not done any code changes). Help me.
Cause of issue:
params = [NSString stringWithFormat:@"query=%@", searchTextField.text];
NSMutableURLRequest *searchRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.example.com/do/m/]];
[searchRequest setHTTPMethod:@"POST"];
[searchRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
If I use the simple request like below, it works fine.
NSURLRequest *searchRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://www.example.com/do/m/?%@", params]] ];
Thanks
Finally, I found that, if the URL is same in the consecutive POST requests for the
loadRequestmethod thencanGoBackdoes not work. It was working for simple GET requests because URLs were different.To fix this, I sent the consecutive POST requests with two URLs, which are different from
UIWebViewperspective but actually same from the server perspective. I am setting the different URLs alternatively for consecutive POST requests, by adding a question-mark(?).https://www.example.com/do/m/
https://www.example.com/do/m/?
In this way the URLs become different for consecutive POST requests of
UIWebViewandcanGoBackmethod works.Still I donot whether it is a bug in
UIWebViewor we can not use consecutive POST requests withUIWebViewto get thecanGoBackmethod works.