我最终通过创建一个 NSMutableArray 数组来跟踪浏览器历史记录并添加代码来处理各种事情(例如前进和后退按钮显示)来修复此错误。我使用 2 个整数(urlListIndex 和 urlIndexMax)以及一个 BOOL(backFix)进行导航控制
- (id)initWithURL:(NSURL*)pageURL {
_retainedURL = pageURL;
[_retainedURL retain];
RKClient *client;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
client = [RKClient clientWithBaseURL: pageURL];
client.username=[userDefaults objectForKey:@"UserName"];
client.password=[userDefaults objectForKey:@"Password"];
client.authenticationType=RKRequestAuthenticationTypeHTTPBasic;
client.disableCertificateValidation= TRUE;
RKRequest *request=[RKRequest requestWithURL:pageURL];
[client configureRequest:request];
request.delegate = self;
request.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
request.disableCertificateValidation = TRUE;
self.authenticatedRequest = request;
[request sendAsynchronously];
//Instantiate urlList (for web history) and urlListIndex
urlList = [[NSMutableArray alloc] init];
urlListIndex = 0;
if(self = [super init]) {
self.URL = pageURL;
self.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsMailLink | SVWebViewControllerAvailableActionsCopyLink;
}
return self;
}
- (void)updateToolbarItems {
//check if you are further than the first page. If so, enable the back button
if (urlListIndex > 0)
{
self.backBarButtonItem.enabled = YES;
}
else
{
self.backBarButtonItem.enabled = NO;
}
//Check if there are more total urls in the list than the one you are on. If so, enable the forward button
if (urlListIndex < maxListIndex)
{
self.forwardBarButtonItem.enabled = YES;
}
else
{
self.forwardBarButtonItem.enabled = NO;
}
//Took out the rest for space
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSURL *urlstring = self.mainWebView.request.URL;
//Check to see if the current url is in the list. If not, add it to the list.
if ([urlList containsObject:urlstring])
{}
else
{
//The first url you click doesnt show up normally so an exception is used here with _retainedURL
if (_retainedURL != nil)
{
[urlList addObject:_retainedURL];
_retainedURL = nil;
}
else
{ if (backFix == NO)
{
urlListIndex++;
[urlList addObject:urlstring];
}
}
}
//Keeps track of the max number of URLs in the list
if (urlListIndex > maxListIndex)
{
maxListIndex = urlListIndex;
}
[self updateToolbarItems];
backFix = NO;
}
- (void)goBackClicked:(UIBarButtonItem *)sender {
backFix = YES;
urlListIndex--;
_request = [NSURLRequest requestWithURL:[urlList objectAtIndex:urlListIndex]];
[mainWebView loadRequest:_request];
}
//Handles the forward button by incrementing the urlListIndex and then loading the url for that index.
- (void)goForwardClicked:(UIBarButtonItem *)sender {
urlListIndex++;
_request = [NSURLRequest requestWithURL:[urlList objectAtIndex:urlListIndex]];
[mainWebView loadRequest:_request];
}