views:

328

answers:

1

I'm working on an application to run base camps website only.

I need it to work when you login and press "Remember me on this computer"

I'm not sure how to set it up to actually remember your login and keep you logged in when you come back to the app.

Is there a way to 'save' the state of the application when you quite it and come back to what you were doing?

Here is an image of the login form on the website used in the application.

http://arikburns.com/forums/fn/IMG_0005.PNG

Thanks.

A: 

If your web application uses url sessions, you can save your current URL using the webview delegate:

- (void)webViewDidFinishLoad:(UIWebView *)myWebView {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:myWebView.request.URL.absoluteString forKey:@"lastUrl"];
    [prefs synchronize];
}

… and load it from the preferences later again when the viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: [prefs objectForKey:@"lastUrl"]]]];
}

You can also access the cookies using javascript, if you need to save and set them:

NSString *myCookies = [[myWebView stringByEvaluatingJavaScriptFromString:@"document.cookie"] copy];
favo
Thanks for the reply.I'm still having trouble.in the second string. I get this warning. "/Users/Aakburns/Desktop/FNOnlineBuilds/Source/WebViewTutorial/Classes/WebViewController.m:92:0 /Users/Aakburns/Desktop/FNOnlineBuilds/Source/WebViewTutorial/Classes/WebViewController.m:92: warning: local declaration of 'webView' hides instance variable"I've changed your 'MyWebView' to webview, as I have it declared. The line with the warner is this. [prefs setObject:webView.request.URL.absoluteString forKey:@"lasturl"];Am I supposed to change the 'lasturl' with the address that the page loads?
Aakburns
I'm also getting and unused variable 'my cookies' on the java string.NSString *myCookies = [[webView stringByEvaluatingJavaScriptFromString:@"document.cookie"] copy];Sorry for the hassle. I've been working with the SDK for about a week now. Catching on quick, but still need a bit of an understanding of things.
Aakburns
If case of the local declaration info you just need to rename the variables. Dont forget to rename the function header too.Regarding the "myCookies", this was just an example line on how to read the cookies into a variable. You still need to work with this variable, it is only a sample on how to access them.
favo
Do I need to put the 'lasturl' as the actual address I want it to 'remember' the user input of username and pass? As far as *myCookies, is there something else I need to setup that it refers to?I really appreciate your help, if you can just get me over this hurdle I'll surely be really learning something that is going to help me have a better understanding of the working here.
Aakburns