투케이2K

394. (ios/swift5) [유틸 파일] getHttpCookieToCookieString : HTTP 쿠키 값을 document cookie 값으로 포맷 실시 본문

IOS

394. (ios/swift5) [유틸 파일] getHttpCookieToCookieString : HTTP 쿠키 값을 document cookie 값으로 포맷 실시

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

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

 

    // -----------------------------------------------------------------------------------------s
    // MARK: - [SEARCH FAST] : getHttpCookieToCookieString : HTTP 쿠키 값을 document cookie 값으로 포맷 실시
    // -----------------------------------------------------------------------------------------
    func getHttpCookieToCookieString(cookie : HTTPCookie?) -> String {
        
        /*
        // -----------------------------------------
        [getHttpCookieToCookieString 메소드 설명]
        // -----------------------------------------
        1. HTTP 쿠키 값을 document cookie 값으로 포맷 실시
        // -----------------------------------------
        2. 호출 방법 :
         
         // [HTTPCookie 변수 선언]
         let authCookie = HTTPCookie(properties: [
             .domain: "twok.com",
             .path: "/",
             .name: "TSEESION",
             .value: "ABCD1234567890",
             .sameSitePolicy: "None",
             .secure: "TRUE"
         ])
         
         
         // [메소드 호출]
         C_WebviewCommonFunc().getHttpCookieToCookieString(cookie: authCookie)
        // -----------------------------------------
        3. 리턴 반환 :
        // -----------------------------------------
         MARK: document.cookie='TSEESION=ABCD1234567890; domain=twok.com; path=/; SameSite=none; Secure;'
        // -----------------------------------------
        */


        // [리턴 변수 선언]
        var returnData = ""
        
        
        // [사전 널 체크 수행]
        if cookie != nil {
            
            // ------------------------------------------
            
            returnData = "document.cookie='"
            
            // ------------------------------------------
            
            // MARK: [세션]
            if cookie?.name != nil && String(describing: cookie?.name.description ?? "").count > 0
                && cookie?.value != nil && String(describing: cookie?.value.description ?? "").count > 0 {
                
                // [리턴 값에 추가]
                returnData += "\(cookie?.name ?? "")=\(cookie?.value ?? ""); "
                
            }
            
            // ------------------------------------------
            
            // MARK: [domain]
            if cookie?.domain != nil && String(describing: cookie?.domain.description ?? "").count > 0 {
                
                // [리턴 값에 추가]
                returnData += "domain=\(cookie?.domain ?? ""); "
                
            }
            
            // ------------------------------------------
            
            // MARK: [path]
            if cookie?.path != nil && String(describing: cookie?.path.description ?? "").count > 0 {
                
                // [리턴 값에 추가]
                returnData += "path=\(cookie?.path ?? ""); "
                
            }
            
            // ------------------------------------------
            
            // MARK: [sameSite]
            if cookie != nil && String(describing: cookie?.description ?? "").count > 0
                && String(describing: cookie?.description ?? "").lowercased().replacingOccurrences(of: " ", with: "").contains("samesite:") == true{
                
                // [문자열 변환]
                var data = String(describing: cookie?.description ?? "").replacingOccurrences(of: "\n\t", with: ";")
                
                // [split 분리]
                var array = data.split(separator: ";")
                
                // [for 문 돌면서 체크 실시]
                if array != nil && array.count > 0 {
                    for i in stride(from: 0, through: array.count-1, by: 1) {
                        
                        if array[i].lowercased().contains("samesite") == true {
                            
                            var item = array[i].lowercased().replacingOccurrences(of: ";", with: "") // [세미 콜론 제거]
                            
                            var getValue = item.split(separator: ":") // [value 파싱]
                            
                            if getValue != nil && getValue.count > 0 {
                                
                                // [리턴 값에 추가]
                                returnData += "SameSite=\(getValue[1].trimmingCharacters(in: .whitespacesAndNewlines)); "
                                
                                break
                                
                            }
                            
                        }
                        
                    }
                }
                
            }
            
            // ------------------------------------------
            
            // MARK: [Secure]
            if cookie?.isSecure != nil && String(describing: cookie?.isSecure.description ?? "").count > 0 {
                
                // [리턴 값에 추가]
                if String(describing: "\(cookie?.isSecure.description ?? "")") != ""
                    && String(describing: "\(cookie?.isSecure.description ?? "")").count > 0
                    && String(describing: "\(cookie?.isSecure.description ?? "")").lowercased().contains("true") == true {
                 
                    //returnData += "Secure=\(cookie?.isSecure.description ?? "");"
                    returnData += "Secure;"
                    
                }
                
            }
            
            // ------------------------------------------
            
            returnData = returnData.trimmingCharacters(in: .whitespacesAndNewlines)
            returnData += "'"
            
            // ------------------------------------------
            
        }

        
        // [로그 출력 실시]
        //*
        S_Log._D_(description: "HTTP 쿠키 값을 document cookie 값으로 포맷 실시", data: [
            "INPUT :: \(String(describing: cookie?.description ?? ""))",
            "RETURN :: \(returnData)"
        ])
        // */
        
        
        // [리턴 데이터 반환 실시]
        return "\(returnData)"
    }

 

반응형
Comments