Im doing an iphone application where i login using OAuth apis of the website for the login process and the status in the uiwebview remains as logged in all the time. I thought clearing the cookies in the uiwebview would help me logout. I want a logout option per say to the app. I have done this to clear all the cookies in uiwebview.
1) i have created a script.js and added it to the project which has the following code
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
2) i have added this code in one of the view controller which has a logout button. As soon as the botton is clicked, this code is executed
- (void)injectJavascript:(NSString *)resource {
NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@"js"];
NSString *js = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
[webView stringByEvaluatingJavaScriptFromString:js];
}
-(void)logoutFromApp
{
[self injectJavascript:@"script"];
}
First of all im not even sure that this code is working. How will i know if it has cleared all the cookies.? Meaning, how will i be notified that it has succeeded clearing or not?
Is what im opting to do is the best way of doing this or are there any other methods to give a logout option?
OK i solved the issue..
this deletes all the cookies in the uiwebview. Works great.