티스토리 뷰

반응형

웹뷰에서 화면의 값을 가져와야할 일이 생겼습니다.

html 바디의 pre 태그 안에 json 데이터가 들어있습니다.

<html>
<body>
    <pre>
        {"status":true,"message":"success","data":{"name":"Gons","url":"https://gonslab.tistory.com/"}}
    </pre>
</body>
</html>

<pre id="data"> 이런식으로 아이디를 넣어주면

document.getElementById(“data") 이렇게 손쉽게 찾을 수 있는데

타사에서 넘겨받은 데이터라 수정할 수가 없어서 태그명으로 접근했습니다.

webView.evaluateJavaScript("document.getElementsByTagName('pre')[0].innerHTML") { html, error in
    
    guard let html = html as? String else {
        print(error as Any)
        return
    }
    print(html)
}

이렇게 태그 경로대로 작성하면 Json 데이터만 가져올 수 있습니다.

{

  "status": "true",

  "message": "success",

  "data": {

    "name": "Gons",

    "url": "https://gonslab.tistory.com/"

  }

}

이제 이 문자열을 모델로 가공해서 사용하시면 됩니다 ㅎㅎ

iOS

Swift

Xcode

반응형
댓글
300x250
반응형
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday