我可以在没有goBack导航的情况下获取UIWebView的先前URL吗?

问题描述 投票:3回答:3

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

[我知道我可以自行实现历史记录堆栈,但是我相信UIWebView会维护历史记录堆栈,以便它可以前后移动。是否可以访问此历史记录?

编辑:我的意思是:是否可以通过从UIWebView访问此历史记录?

ios uiwebview history
3个回答
2
投票

[我喜欢RAJA曾说过将URL存储在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
投票

您可以做的一件事是,您可以通过获取WebView的绝对URL来存储加载到WebView中的所有URL。将[[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


0
投票

使用下面的代码访问最后一个URL

webView.backForwardList.backItem?.url
© www.soinside.com 2019 - 2024. All rights reserved.