我可以在没有goBack导航的情况下获得UIWebView的上一个url吗?

时间:2014-03-20 10:05:11

标签: ios uiwebview history

如果是UIWebView canGoBack,我是否可以在没有goBack导航的情况下获取上一个网址?

我知道我可以实现自己的历史堆栈,但我相信UIWebView维护一个历史堆栈,以便它可以前后移动。 有没有办法访问这段历史?

编辑:我的意思是:有没有办法从UIWebView 访问此历史

3 个答案:

答案 0 :(得分:2)

我喜欢RAJA关于将网址存储在NSArray中的说法,但他实际上并没有这样做,所以现在就去。

<强> MySubClass.h

@interface MySubClass : MySuperClass

// I am assuming that you are creating your UIWebView in interface builder
@property (nonatomic, strong) IBOutlet UIWebView *myWebView;    

// I'm going to make the initial array that stores the URLs private to
// this class but if we wanted to access that array outside of this class
// we can using this method, but we will return a NSArray not a NSMutableArray
// so it can't be modified outside of this class.
- (NSArray *)visitedURLs;

@end

<强> MySubClass.m

#import "MySubClass.h"

// Our private interface   
@interface MySubClass()

// Our private mutable array
@property (nonatomic, strong) NSMutableArray *visitedURLsArray;  

@end  

@implementation MySubClass

- (void)viewDidLoad
{ 
    // Check if the visitedURLsArray is nil and if it is alloc init
    if(visitedURLsArray == nil) 
        visitedURLsArray = [[NSMutableArray alloc] init];
}

- (NSArray *)visitedURLs
{
    return (NSArray *)visitedURLsArray;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // If you don't wish to store the URL every time you hit refresh where you
    // could end up with the same URL being add multiple times you could also 
    // check what the last URL was in the array and if it is the same ignore it
    if(![[visitedURLsArray lastObject] isEqualToString:[[request URL] absoluteString]]) {

        // Every time this method is hit, so every time a request is sent into the 
        // the webView. We want to store the requested URL in the array.
        // so this would create a log of visited URLs with the last one added being
        // the last URL visited. 
        [visitedURLsArray addObject:[[request URL] absoluteString]];
    }
    return YES;
}

@end

重要提示

此答案基于原始问题,该问题并未提及与用户正在使用的backbone.js有任何关系。

答案 1 :(得分:1)

您可以做的一件事是,您可以通过获取webview的绝对网址来存储加载到网页视图中的所有网址。将[[request URL] absoluteString]存储在一个数组中,并根据需要使用该URL。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"url %@", [[request URL] absoluteString]);

return YES;
}

或者您可以查看此链接以查看chache webview网址。此链接可能会对您有所帮助 - Listen to all requests from UIWebView

答案 2 :(得分:1)

使用以下代码访问最后一个网址

webView.backForwardList.backItem?.url