0

所以我遇到的问题是我正在使用 RestKit 来验证自签名证书并同时处理用户登录但是在 webView 打开之后第一页没有被添加到后退列表中(据我所知)。即,在我单击该页面上的链接后,我无法返回上一页。如果我单击另一个链接(3 页),我可以返回到第二页但仍然不是第一页。当我打开 UI Webview 时,有没有办法手动将 URL 添加到后退列表?或者对其他解决方法有什么建议?

- (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;
    //The below "delegate:self seems to be what is preventing the URL from passing into the forwardBack list (works if it is delegate self but then RestKit breaks
    RKRequest *request=[RKRequest requestWithURL:pageURL delegate:self];
    [client configureRequest:request];
    request.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
    request.disableCertificateValidation = TRUE;
    self.authenticatedRequest = request;
    NSLog(@"%@",[request URLRequest]);
    [request sendAsynchronously];

    if(self = [super init]) {
        self.URL = pageURL;
        self.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsMailLink | SVWebViewControllerAvailableActionsCopyLink;
    }
    return self;
}
4

1 回答 1

0

我最终通过创建一个 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];

}
于 2012-12-03T23:54:40.957 回答