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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With