[swift] Swift Presentr를 사용한 웹뷰 구현 방법

개요

이번 글에서는 Swift Presentr라이브러리를 사용하여 iOS 앱에서 웹뷰를 구현하는 방법에 대해 알아보겠습니다. Presentr는 앱 내에서 커스텀화된 모달 프레젠테이션을 손쉽게 구현할 수 있는 라이브러리입니다. 웹뷰를 모달로 표시하고 싶은 경우, Presentr는 매우 유용한 도구가 될 수 있습니다.

단계별 구현

1. Presentr 라이브러리 설치

Presentr를 사용하기 위해 먼저 Presentr 라이브러리를 설치해야합니다. CocoaPods를 사용하는 경우, Podfile에 아래의 코드를 추가하고 pod install을 실행하세요.

pod 'Presentr'

2. 웹뷰 컨트롤러 만들기

다음으로, 웹뷰를 표시할 뷰 컨트롤러를 만듭니다. 이 뷰 컨트롤러는 WKWebView를 사용하여 웹페이지를 로드하고 표시하는 역할을 합니다. 아래와 같이 코드를 작성합니다.

import UIKit
import WebKit

class WebViewController: UIViewController {
    var webView: WKWebView!
    var url: URL!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        view = webView
        
        let request = URLRequest(url: url)
        webView.load(request)
    }
}

3. Presentr 설정하기

이제 Presentr를 사용하여 웹뷰를 표시하기 위한 Presentr 객체를 만들고 설정합니다. 아래와 같이 코드를 작성합니다.

import UIKit
import Presentr

class ViewController: UIViewController {
    let presenter: Presentr = {
        let width = ModalSize.custom(size: 300)
        let height = ModalSize.custom(size: 400)
        let center = ModalCenterPosition.customOrigin(origin: CGPoint(x: 0, y: 100))

        let customPresenter = Presentr(presentationType: .popup)
        customPresenter.transitionType = .coverHorizontalFromRight
        customPresenter.dismissTransitionType = .coverHorizontalFromRight
        customPresenter.roundCorners = true
        customPresenter.backgroundOpacity = 0.5
        customPresenter.dismissOnSwipe = true
        customPresenter.dismissOnSwipeDirection = .bottom
        customPresenter.keyboardTranslationType = .moveUp
        customPresenter.presentationType = .popup
        customPresenter.transitionType = .coverHorizontalFromRight
        customPresenter.dismissTransitionType = .coverHorizontalFromRight
        customPresenter.dismissAnimated = true
        customPresenter.blurBackground = true
        customPresenter.blurStyle = .dark
        customPresenter.animateInTime = 0.3
        customPresenter.animateOutTime = 0.3
        customPresenter.width = width
        customPresenter.height = height
        customPresenter.center = center

        return customPresenter
    }()
    
    @IBAction func showWebview(_ sender: UIButton) {
        let webVC = WebViewController()
        webVC.url = URL(string: "https://www.example.com")
        
        customPresentViewController(presenter, viewController: webVC, animated: true, completion: nil)
    }
}

위의 코드에서 Presentr 객체를 생성하고 원하는 모달 스타일 및 효과를 설정할 수 있습니다. showWebview 액션에서는 원하는 웹페이지 URL을 WebViewController 객체의 url 프로퍼티에 설정한 후 모달로 표시합니다.

4. 실행하기

위의 코드를 실행하여 버튼을 누르면 모달 창으로 웹뷰가 표시되는 것을 확인할 수 있습니다.

결론

이번 글에서는 Swift Presentr 라이브러리를 사용하여 iOS 앱에서 웹뷰를 구현하는 방법에 대해 알아보았습니다. Presentr를 사용하면 매우 간편하게 모달로 웹뷰를 표시할 수 있습니다. Presentr의 다양한 설정 옵션을 사용하여 원하는 스타일 및 효과를 적용할 수 있습니다. Presentr는 iOS 앱 개발에서 모달 프레젠테이션을 구현할 때 유용한 라이브러리입니다.