웹의 복잡한 콘텐츠를 렌더링하는 가장 쉬운 방법은 거의 항상 WKWebView를 사용하는 것이다.
import UIKit
import WebKit
class DetailViewController: UIViewController {
var webView: WKWebView!
var detailItem: Petition?
override func loadView() {
webView = WKWebView()
view = webView
}
그러나 petition 첵스트를 웹 뷰에 바로 넣으면 작게 보일 것이기 때문에 HTML로 래핑해야한다.
guard let detailItem = detailItem else { return }
let html = """
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style> body { font-size: 150%; } </style>
</head>
<body>
\(detailItem.body>
</body>
</html>
"""
webView.loadHTMLString(html, baseURL: nil)
HTML은 width는 기기의 width와 같고, 폰트 사이즈는 기본 폰트 사이즈의 150%로 지정한다.
모든 HTML은 청원의 본문값과 결합된 후에 webView로 전송된다.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
SceneDelegate.swift 파일의 아래 메서드에 다음과 같이 추가해준다.
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let tabBarController = window?.rootViewController as? UITabBarController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "NavController")
vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
tabBarController.viewControllers?.append(vc)
}
}