viewport (자바스크립트를 웹뷰에 적용시키기)

https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html

https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag

문제

웹뷰를 띄웠을 때 너무 작다

해결

<meta name="viewport" content="width=device-width">

위 html을 웹뷰가 로드될 때 추가해주자

//script.js
var meta = document.createElement('meta');
    meta.name = "viewport";
    meta.content = "width=device-width";

document.getElementsByTagName('head')[0].appendChild(meta);
    override func loadView() {
        var content = ""
        if let path = Bundle.main.path(forResource: "script", ofType: "js") {
            do {
                content = try String(contentsOfFile: path)
            } catch {
                print("error")
            }
        }
        
        // 자바스크립트 웹뷰에서 실행시키기
        let webConfiguration = WKWebViewConfiguration()
        
        let userScript = WKUserScript(source: content, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
        let contentController = WKUserContentController()
        contentController.addUserScript(userScript)
        webConfiguration.userContentController = contentController
        
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

결과

좋은 웹페이지 즐겨찾기