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

后端 未结 2 1871
北荒
北荒 2020-12-17 06:20

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 we

2条回答
  •  庸人自扰
    2020-12-17 06:52

    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

提交回复
热议问题