PROGRAMMING CODE/SWIFT
[Swift] 앱에서 웹사이트 열기(Type 'AppleSupportView' does not conform to protocol )
daye_
2023. 8. 5. 20:24
ContentView.swift
import SwiftUI
struct ContentView: View {
@state private var showingWebSheet : Bool = false
var body: some View {
NavigationStack{
Button {
showingWebSheet = true
} label: {
Text("Open Website").font(.largeTitle).bold()
}
}
.sheet(isPresented: $showingWebSheet) { //sheet로 사이트 열기
SafariWebView(siteURL: "")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
SafariWebView.swift
import Foundation
import SwiftUI
import SafariServices
//UIViewControllerRepresentable UIViewController 연결가능하게 하는 프로토콜
struct SafariWebView : UIViewControllerRepresentable{
var siteURL : String
func makeUIViewController(context: Context) -> some UIViewController {
return SFSafariViewController(url: URL(string:siteURL) ?? URL(string: "https://developer.apple.com/")!)
}
}
현재는 ContentView의 SafariWebView(siteURL: [이부분])에 아무 URL도 넣어주지 않았기 때문에
return 부분에서 옵셔널 바인딩 시도 후 nil값일때 디폴트로 넣어준 apple홈페이지가 열리게 되는것!
추가로 핸들링 코드 넣어주면 좋겠따.
+ 최근에 다시 써보려고 열었는데
Type 'AppleSupportView' does not conform to protocol 'UIViewControllerRepresentable' 라는 오류가 떴다.
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
업데이트 할게 없더라도 선언만 해주면 ㅇㅋ
UIViewControllerRepresentable 프로토콜을 따르지 않았다고 함!