Project 21 - WebView

DaY·2021년 5월 21일
1

iOS

목록 보기
46/52
post-thumbnail

어플리케이션 화면에 web 띄우기

스토리보드의 controller에 wkWebView를 삽입한다.

아래와 같이 원하는 url를 request, load 한다.

webView.load(url)
extension WKWebView {
    func load(_ urlString: String) {
        if let url = URL(string: urlString) {
            let request = URLRequest(url: url)
            load(request)
        }
    }
}

webview에는 따로 뒤로가기, 새로고침 등의 버튼이 없기 때문에 따로 ui를 생성해 준 후 Action을 추가해주어야 한다.

@IBAction func back(_ sender: Any) {
    webView.goBack()
}
    
@IBAction func forward(_ sender: Any) {
    webView.goForward()
}
    
@IBAction func reload(_ sender: Any) {
    webView.reload()
}

plist 권한 설정

plist를 설정하지 않으면 다음과 같은 오류가 발생한다.

Cannot convert value of type 'String' to expected argument type 'URLRequest'

App Transport Security Settings를 입력하고 하위에 Allow Arbitary Loads를 허용해줌으로써 해결 가능하다.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

해당 부분은 이 게시물에서 다룬 적이 있다.

0개의 댓글