투케이2K

393. (ios/swift5) [유틸 파일] httpUrlCacheClear : http 통신 URL 캐시 초기화 수행 - URLSession , URLCache 본문

IOS

393. (ios/swift5) [유틸 파일] httpUrlCacheClear : http 통신 URL 캐시 초기화 수행 - URLSession , URLCache

투케이2K 2023. 12. 18. 19:15
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

 

    // -----------------------------------------------------------------------------------------
    // MARK: - http 통신 URL 캐시 초기화 수행
    // -----------------------------------------------------------------------------------------
    func httpUrlCacheClear() {
        
        /*
        // -------------------------------------------------------
        [httpUrlCacheClear 메소드 설명]
        // -------------------------------------------------------
        1. http 통신 URL 캐시 초기화 수행
        // -------------------------------------------------------
        2. 호출 방법 :
         
         C_App().httpUrlCacheClear()
         
        // -------------------------------------------------------
        */
        
        
        // -------------------------------------------------------
        // MARK: [URLSession 및 URLCache 제거]
        // -------------------------------------------------------

        // [설치된 날짜 가져오는 코드]
        let urlToDocumentsFolder: URL? = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
        let installDate = try? FileManager.default.attributesOfItem(atPath: (urlToDocumentsFolder?.path)!)[.creationDate] as? Date
        
        // [설치된 날짜부터 지금까지의 cookie all clear]
        URLSession.shared.configuration.httpCookieStorage?.removeCookies(since: installDate!)
        
        // [URL 요청 후 잔여 캐시 데이터 삭제]
        URLCache.shared.removeAllCachedResponses()
        
        // [HTTPCookieStorage 데이터 삭제]
        HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)

        // -------------------------------------------------------


        // [로그 출력 실시]
        //*
        S_Log._D_(description: "URLSession / URLCache / HTTPCookieStorage :: http 통신 URL 캐시 및 쿠키 삭제 수행", data: nil)
        // */
        
    }

 

반응형
Comments