how to remove html5 local storage of an iOS app using UIWebview

我只是一个虾纸丫 提交于 2019-12-18 04:24:08

问题


I have an app that uses UIWebview which is used to browser different sites/urls. I am in need of an option "clear client data" similar to iOS mobile safari "Remove All website Data" that removes cookies/local storage. I know how to do the cookies but i am unable to figure out how to remove/clear the later (local storage)

How to remove HTML5 persistant databases in UIWebView? discussed this but i didn't see how that works for my above case. localStorage.clear() javascript needs to be run in UIWebview stringByEvaluatingJavaScriptFromString() method but this didn't seem to have worked in my testing. The other concern with the above approach is that, localStorage is url/site specific - but i want to clear all the contents inside localStorage.

Tried removing/copying Appln/Library/WebKit/LocalStorage folder - but this didnt work out with access restriction errors.

Any suggestions/directions please ?


回答1:


You should be able to do it cleaning the content of the Caches directory of your app:

EDIT: Update code as suggested by @RaphaelSchweikert because of wrong error management.

// first, remove the cookies. 
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
    [storage deleteCookie:cookie];
}

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
for (NSString *string in array) {
    NSLog(@"Removing %@", [path stringByAppendingPathComponent:string]);
if ([[string pathExtension] isEqualToString:@"localstorage"])
    [[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:string] error:nil];
}

Inside that folder on my simulator I see many .localstorage files ;-) Try and let me know




回答2:


Note, that, if you’re using PhoneGap, you’ll also have to clear their internal backup of the localstorage cache:

//Remove the localstorage db
NSString *path = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Backups"] stringByAppendingPathComponent:@"localstorage.appdata.db"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

//Also remove the cached versions
path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
for (NSString *string in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]) {
    if ([[string pathExtension] isEqualToString:@"localstorage"]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
}


来源:https://stackoverflow.com/questions/8500334/how-to-remove-html5-local-storage-of-an-ios-app-using-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!