关闭后,从UIWebView释放内存/ cookie /缓存

时间:2012-02-25 16:10:04

标签: ios caching memory-management uiwebview

我有UIViewController,其中包含UIWebView

我使用UIViewController中的UIWebview登录到一个说facebook的网站然后我点击完成,这解雇了视图控制器,我想这将释放视图控制器。但是当我再次打开UIWebview时(没有退出应用程序,甚至在终止应用程序之后),网页仍然会在加载Web视图后登录到Facebook。我应该如何发布网页视图,以便每次点击完成并返回到网页视图时,网页视图将始终像“全新”,而不是登录到我之前登录过的任何网站。

- (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];
}

我试过这个但是没有用:

-(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];

}

我也试过这个没有任何成功:

- (void) viewDidDisappear:(BOOL)animated {

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

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

1 个答案:

答案 0 :(得分:2)

得到了论坛上某人的回答,所以归功于他......

将此代码放在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];

}