【问题标题】:Can I get previous url of a UIWebView without goBack navigation?我可以在没有 goBack 导航的情况下获取 UIWebView 的先前网址吗?
【发布时间】:2014-03-20 10:05:10
【问题描述】:

如果是UIWebViewcanGoBack,我可以在没有返回导航的情况下获取上一个网址吗?

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

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

【问题讨论】:

标签: ios uiwebview history


【解决方案1】:

我喜欢 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 有任何关系。

【讨论】:

  • 首先,您的解决方案不适用于使用backbone.js 的网页,因为它们不会触发shouldStartLoadWithRequest:。其次,管理导航并不像您想象的那么简单。您必须确定用户是后退还是前进,然后将其推入堆栈或从堆栈中弹出。
  • backbone.js 这是一条非常重要的信息,我们需要知道错过它意味着我们无法为您提供您真正想要的答案。此代码已经过测试并且可以工作,它将记录用户前进或后退的历史记录。如果您想要通过它的声音做的其他事情,那么错过对我们回答您的问题非常重要的信息是您自己的错,例如您使用backbone.js 非常重要,因为它完全改变了给出的答案。
  • @WeiLiu 请提出正确的问题,提供正确的信息,不要再浪费我们的时间了。
  • 如果我没有把问题说清楚,我真的很抱歉。无论是否使用backbone.js,您的答案都没有从历史数组中removeObject,这显然是不正确的。当用户返回然后导航到不同的页面时,您打算如何删除一条记录?
  • 抱歉浪费了您的时间。你让我哈哈大笑。
【解决方案2】:

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

webView.backForwardList.backItem?.url

【讨论】:

  • @WeiLiu 没有人可以选择他们的出生地。我住在伊朗,作为一名程序员有很多限制,但我从不感到沮丧。我庞大的解决方案解决了您的问题吗?
【解决方案3】:

您可以做的一件事是,您可以通过获取 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 url。此链接可能对您有所帮助 - Listen to all requests from UIWebView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2013-01-23
    • 1970-01-01
    • 2019-09-17
    • 2018-01-07
    • 2020-02-16
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多