【问题标题】:Release memory/cookie/cache from UIWebView once closed关闭后从 UIWebView 释放内存/cookie/缓存
【发布时间】:2012-03-15 18:46:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios caching memory-management uiwebview


    【解决方案1】:

    从论坛上的某个人那里得到答案,所以感谢他...

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

    【讨论】:

      猜你喜欢
      • 2014-11-17
      • 2016-02-09
      • 2013-01-02
      • 2011-09-18
      • 2013-02-11
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多