3

If a UIWebView canGoBack, can I get the previous url without a goBack navigation?

I know I can implement a history stack my self, but I believe that UIWebView maintains a history stack so it can go back and forward. Is there a way to access this history?

EDIT: I mean: Is there a way to access this history from UIWebView?

Wei Liu
  • 555
  • 8
  • 24
  • 1
    Apple introduced WKWebView from iOS 8. It has a readonly property ```backForwardList``` https://developer.apple.com/library/prerelease/ios/documentation/WebKit/Reference/WKWebView_Ref/index.html#//apple_ref/occ/instp/WKWebView/backForwardList – Wei Liu Jun 17 '14 at 05:52

3 Answers3

2

I like the idea that RAJA has said about storing the URLs in an NSArray but he hasn't actually implemented it so here you go.

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

Important Note

This answer is based on original question that mentions nothing of anything to do with backbone.js which it turns out the user is using.

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • 1
    First, your solution won't work with web pages using backbone.js, as they don't trigger `shouldStartLoadWithRequest:`. Second, managing navigation is not as simple as you thought. You have to identify whether the user is going back or forward and push into or pop from the stack. – Wei Liu Mar 31 '14 at 06:30
  • `backbone.js` This is a piece of information that is very important that we need to know missing this out means we can't give you the answer that you actually want. This code has been tested and works, it will record history of where the user goes whether it forward or backwards. If you wanted something else which you did by the sounds of it, it is your own fault for missing out information that is very important to us answering your question such as you are using `backbone.js` very important as it changes the answers given completely. – Popeye Mar 31 '14 at 08:34
  • @WeiLiu please go an ask the correct question providing the correct information and stop wasting our time by not doing so. – Popeye Mar 31 '14 at 08:35
  • I am really sorry if I did not make the question clear. Regardless of using backbone.js or not, your answer did not **removeObject** from the history array at all, which is not correct apparently. How do you plan to remove one record when user goes back and then navigate to a different page? – Wei Liu Mar 31 '14 at 08:58
  • @WeiLiu This isn't what you ask though is it, you mention nothing of removing when going back in your question so I haven't implemented this. The way Stackoverflow works is you provide information and we provide answers based on the requirements in your question and the information you provide us. If you miss something out that is your own fault, we provide answers to the information given so this answer works based on the information provided. If you update your question to include this when I have time (This evening) I will provide an update to my answer. – Popeye Mar 31 '14 at 09:19
  • 2
    Sorry for wasting your time. You've made me LOL. – Wei Liu Mar 31 '14 at 09:34
1

One thing you can do is, you can store all the url's that loads in your webview by getting absolute url of the webview. Store the [[request URL] absoluteString] in an array and use the url as required.

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

return YES;
}

Or you can check this link for chache webview url. this link may help you - Listen to all requests from UIWebView

Community
  • 1
  • 1
RAJA
  • 1,214
  • 10
  • 13
1

use below code to access last url

webView.backForwardList.backItem?.url
Masoud Roosta
  • 455
  • 9
  • 19
  • 1
    @WeiLiu No one can choose their birthplace. I live in Iran and have many limitations as a programmer but I never get frustrated. Did my bulky solution solve your problem? – Masoud Roosta Jan 28 '20 at 10:47