简体   繁体   中英

Loading UIWebview with last accessed page

I am working on an app in which I need to load the UIWebview with the last accessed webpage. I store the last page URL with :

self.currentURL = [self.webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];

which works fine.

But the problem is , I am not sure on how to save this value so that once I exit the UIWebview and relaunch it , this value is not nil.

So , I tried to store it in NSUserDefaults like this:

[[NSUserDefaults standardUserDefaults] setObject:self.currentURL forKey:@"currentURL"];

this stores the value and I can access it on relaunch of the UIWebview.

This is how I launch my web view:

self.currentURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"currentURL"];

 if (self.currentURL) { [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]]; } else { [webView loadRequest:[NSURLRequest requestWithURL:url]]; } 

which works fine.

Now the problem I am facing is ,

I have the main screen of the my app which has several buttons which open various links (different UIwebview pages).I want to open the UIWebview with the last accessed page only if the page has been navigated and the same button is pressed to open the UIWebView. If the click on any other button , the last accessed page should not be opened , rather a new link which is coded for that button should open.

Does anyone know how to achieve this behaviour?

PS: I am not using XIB in my app.

我建议您使用NSURLCache来存储缓存,然后根据请求的URL加载缓存。

Its not that hard. Let assume you have 4 buttons.

The keep track of each button separately on NSUserDeafults .

Like this:

[[NSUserDefaults standardUserDefaults] setObject:self.currentURL forKey:@"currentURLForButton1"];
[[NSUserDefaults standardUserDefaults] setObject:self.currentURL forKey:@"currentURLForButton2"];
[[NSUserDefaults standardUserDefaults] setObject:self.currentURL forKey:@"currentURLForButton3"];
[[NSUserDefaults standardUserDefaults] setObject:self.currentURL forKey:@"currentURLForButton4"];

In your button1 click action :

self.currentURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"currentURLForButton1"];

if (self.currentURL) {
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]];
}
else {
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

....

....

In button 4 click action:

self.currentURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"currentURLForButton4"];

if (self.currentURL) {
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]];
}
else {
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
}

I just described an easy but lengthy process. You can accomplish this using a single array. Hope this helps.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM