Search
Duplicate

Safari 삽입

상태

애플리케이션 내부에 사파리를 삽입해 보자.

1. 새로운 swift 파일을 생성하고 UIViewControllerRepresentable 프로토콜을 채택해주자.

import SwiftUI import SafariServices struct SfSafariVIewWrapper: UIViewControllerRepresentable { let url: URL func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> SFSafariViewController { return SFSafariViewController(url: url) } func updateUIViewController(_ uiViewController: SFSafariViewController, context: Context) { return } }
Swift
복사
두개의 함수를 구현해주자.
fullScreenCover 메서드를 사용하면 꽉 찬화면의 모달뷰를 띄울 수 있다.
isPresented 파라미터에 바인딩된 불리언 변수를 넣어준다.
A binding to a Boolean value that determines whether to present the sheet.
onDismiss 모달뷰가 dismiss 즉 해제 될 때 실행할 클로져를 전달
The closure to execute when dismissing the modal view.
content 모달뷰가 포함할 컨텐츠를 넣어준다.
A closure that returns the content of the modal view.
.fullScreenCover(isPresented: $showSafari, content: { SfSafariVIewWrapper(url: URL(string: "https://www.designcode.io")!) })
Swift
복사
UIViewControllerRepresentable 프로토콜
UIKit 의 뷰 컨트롤러를 나타내는 프로토콜이다.
SwiftUI 환경에서 UIKit 의 UIViewController 를 사용할 수 있게 해주는 프로토콜이다.

Final Code

struct ContentView: View { @State private var showSafari: Bool = false var body: some View { Text("Hello, world!") .padding() .foregroundColor(.blue) .onTapGesture { showSafari.toggle() } .fullScreenCover(isPresented: $showSafari, content: { SfSafariVIewWrapper(url: URL(string: "https://www.designcode.io")!) }) } }
Swift
복사