Programing

UIWebview 캐시 지우기

lottogame 2020. 11. 13. 07:44
반응형

UIWebview 캐시 지우기


UIWebview를 사용하여 loadRequest:메서드를 사용하여 웹 페이지를로드했습니다. 해당 장면을 떠날 때 [self.webView stopLoading];webView를 호출 하고 릴리스합니다.

처음 시작할 때 활동 모니터에서 실제 메모리가 4MB 증가하고 여러 번 시작 /로드 할 때 실제 메모리가 증가하지 않는 것을 확인했습니다. 한 번만 증가합니다.

webview의 보유 횟수를 확인했습니다. 즉, 0입니다. UIWebView가 일부 데이터를 캐시하고 있다고 생각합니다. 캐싱을 방지하거나 캐시 된 데이터를 제거하려면 어떻게합니까? 아니면 다른 이유가 있습니까?


나는 실제로 당신이 UIWebView. 나는 제거 해봤 UIWebView내에서 UIViewController다음 새로운 하나를 만들어, 그것을 해제. 새 주소는 모든 것을 다시로드 할 필요없이 주소로 돌아 갔을 때 정확히 어디에 있었는지 기억했습니다 (이전 UIWebView에 로그인 한 것을 기억 했습니다).

그래서 몇 가지 제안 :

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];

이렇게하면 특정 요청에 대해 캐시 된 응답이 제거됩니다. 에서 실행 된 모든 요청에 ​​대해 캐시 된 모든 응답을 제거하는 호출도 있습니다 UIWebView.

[[NSURLCache sharedURLCache] removeAllCachedResponses];

그 후에 다음과 관련된 모든 쿠키를 삭제할 수 있습니다 UIWebView.

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

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

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}

스위프트 3 :

// Remove all cache 
URLCache.shared.removeAllCachedResponses()

// Delete any associated cookies     
if let cookies = HTTPCookieStorage.shared.cookies {
    for cookie in cookies {
        HTTPCookieStorage.shared.deleteCookie(cookie)
    }
}

캐싱을 완전히 비활성화하지 마십시오. 앱 성능이 저하되고 불필요합니다. 중요한 것은 앱 시작시 캐시를 명시 적으로 구성하고 필요한 경우 제거하는 것입니다.

따라서 application:DidFinishLaunchingWithOptions:다음과 같이 캐시 제한 구성하십시오.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];

    // ... other launching code
}

올바르게 구성한 후 캐시를 제거해야 할 때 (예 :에서 또는를 applicationDidReceiveMemoryWarning닫을 때 UIWebView) 다음을 수행하십시오.

[[NSURLCache sharedURLCache] removeAllCachedResponses];

기억이 회복되는 것을 볼 수 있습니다. 이 문제에 대해 http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/


다음을 수행하여 캐싱을 비활성화 할 수 있습니다.

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

스위프트 3.

// Remove all cache 
URLCache.shared.removeAllCachedResponses()

// Delete any associated cookies     
if let cookies = HTTPCookieStorage.shared.cookies {
    for cookie in cookies {
        HTTPCookieStorage.shared.deleteCookie(cookie)
    }
}

내 추측은 당신이보고있는 메모리 사용은 페이지 콘텐츠가 아니라 UIWebView를로드하고 모든 것이 WebKit 라이브러리를 지원하는 것입니다. 저는 UIWebView 컨트롤을 좋아하지만 매우 큰 코드 블록을 가져 오는 '무거운'컨트롤입니다.

이 코드는 iOS Safari 브라우저의 큰 하위 집합이며 많은 정적 구조를 초기화합니다.


I could not change the code, so I needed command line for testing purpose and figured this could help someone:

Application specific caches are stored in ~/Library/Caches/<bundle-identifier-of-your-app>, so simply remove as below and re-open your application

rm -rf ~/Library/Caches/com.mycompany.appname/


After various attempt, only this works well for me (under ios 8):

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1 diskCapacity:1 diskPath:nil];
[NSURLCache setSharedURLCache:cache];

For swift 2.0:

let cacheSizeMemory = 4*1024*1024; // 4MB
let cacheSizeDisk = 32*1024*1024; // 32MB
let sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath: "nsurlcache")
NSURLCache.setSharedURLCache(sharedCache)

I am loading html pages from Documents and if they have the same name of css file UIWebView it seem it does not throw the previous css rules. Maybe because they have the same URL or something.

I tried this:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

I tried this :

[[NSURLCache sharedURLCache] removeAllCachedResponses];

I am loading the initial page with:

NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];

It refuses to throw its cached data! Frustrating!

I am doing this inside a PhoneGap (Cordova) application. I have not tried it in isolated UIWebView.

Update1: I have found this.
Changing the html files, though seems very messy.

참고URL : https://stackoverflow.com/questions/5468553/clearing-uiwebview-cache

반응형