打开 UI webview 时,RestKit 未将 URL 添加到后退列表
Posted
技术标签:
【中文标题】打开 UI webview 时,RestKit 未将 URL 添加到后退列表【英文标题】:RestKit not adding URL to backForward list when opening UI webview 【发布时间】:2012-11-20 19:20:16 【问题描述】:所以我遇到的问题是我正在使用 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;
【问题讨论】:
【参考方案1】:我最终通过创建一个 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];
【讨论】:
以上是关于打开 UI webview 时,RestKit 未将 URL 添加到后退列表的主要内容,如果未能解决你的问题,请参考以下文章