一旦关闭,从UIWebView释放内存/cookie/缓存 [英] Release memory/cookie/cache from UIWebView once closed

查看:61
本文介绍了一旦关闭,从UIWebView释放内存/cookie/缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController ,其中包含一个 UIWebView .

I have a UIViewController which contains a UIWebView.

我使用UIViewController中的UIWebview登录到要说Facebook的网站,然后单击完成,这将关闭视图控制器,我想这将释放视图控制器.但是当我再次打开UIWebview(不退出应用程序,甚至不终止应用程序后)时,在加载Web视图后,该网页仍会登录Facebook.我应该如何释放Web视图,以便每次单击完成"并返回到Web视图时,Web视图将始终像全新",而不是登录到我以前登录过的任何网站.

i login to a website for say facebook using the UIWebview in the UIViewController then i click on done, which dismiss the view controller, which i suppose will release the view controller. but when i open the UIWebview again (without exiting the app and also even after terminating the app) the webpage is still log into Facebook after the web view loads. How should i release the web view so that every time i click on done and return back to the web view, the web view will always be like "brand new" not log-on to any website that i had previously log-on to.

- (void)viewDidLoad{
       NSLog(@"webView viewDidLoad called");
       appDelegate = (LoginDBAppDelegate *)[[UIApplication sharedApplication] delegate];
       loginObj = [appDelegate.loginArray objectAtIndex:currentSelectedRow];

       myWebView.delegate = self;
        [super viewDidLoad];
        [self showWebView]; 

}

-(void)showWebView{
    NSLog(@"webView showWebView called");

    NSURL *url = [NSURL URLWithString:loginObj.loginURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    [myWebView loadRequest:request];
}


-(IBAction)done_Clicked:(id)sender{
    NSLog(@"webView Done_Clicked");

    //dismiss the controller
    [self.navigationController dismissModalViewControllerAnimated:YES];

}

-(void)dealloc{
        [myWebView release];
        myWebView.delegate = nil;
        [activity release];
        [urlTextField release];
        [super dealloc];
}

我已经尝试过了,但是没有用:

I have tried this but it didn't work:

-(IBAction)done_Clicked:(id)sender{
NSLog(@"webView Done_Clicked");

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[myWebView stringByEvaluatingJavaScriptFromString:@"var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';"];
[myWebView stringByEvaluatingJavaScriptFromString:@"document.open();document.close()"];

//dismiss the controller
[self.navigationController dismissModalViewControllerAnimated:YES];

}

我也尝试了此尝试,但没有成功:

I have also tried this without any success:

- (void) viewDidDisappear:(BOOL)animated {

NSLog(@"webView viewDidDisappear called");
[super viewDidDisappear:animated];

[self.myWebView removeFromSuperview];
self.myWebView.delegate = nil;
self.myWebView = nil;
[myWebView release];
}

推荐答案

从论坛上某人那里得到答案,所以要相信他...

Got the answer from someone on the forum, so credit to him...

将此代码放入viewDidLoad

Place this code in viewDidLoad

//Set Cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

//Clear All Cookies
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    //if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];

}

这篇关于一旦关闭,从UIWebView释放内存/cookie/缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆