-
#15. 로또앱 만들기(QR Code)SwiftUI 앱만들기 2022. 7. 8. 13:55
#1_1. 설정하기 (info.pilist를 source code로 열기 위해 만든다)
#1_2. 카메라 권한 설정
#1_3. 웹뷰 접근 권한
#2_1. CodeScannerView (출처:https://github.com/twostraws/CodeScanner)
#2_2. CodeScannerView (출처:https://github.com/twostraws/CodeScanner)
#2_3.CodeScannerView (출처:https://github.com/twostraws/CodeScanner)
#3_1. QRCodeGuideLineView, QR코드 가이드 라인 설정
import SwiftUI
struct QRCodeGuideLineView : View{
var body: some View{
GeometryReader{ geometryProxy in
RoundedRectangle(cornerRadius: 20)
.stroke(style: StrokeStyle(lineWidth: 10, dash: [11]))
.frame(width: geometryProxy.size.width / 2, height: geometryProxy.size.height / 3)
.foregroundColor(Color.yellow)
.position(CGPoint(x: geometryProxy.frame(in: .local).midX, y: geometryProxy.frame(in: .local).midY))
}
}
}
struct QRCodeGuideLineView_Previews: PreviewProvider {
static var previews: some View {
QRCodeGuideLineView()
}
}
#3_2. QRCodeScannerExampleViewimport SwiftUI
struct QRCodeScannerExampleView: View {
@State private var isPresentingScanner = false
@State private var scannedCode: String?
var body: some View {
ZStack {
if self.scannedCode != nil{
//Text("self.scannedCode : \(self.scannedCode!)")
MyWebView(urlToLoad: self.scannedCode!)
} else {
MyWebView(urlToLoad: "https://www.naver.com")
}
VStack {
Spacer()
Button(action:{
self.isPresentingScanner = true
}){
Text("로또번호확인")
.font(.system(size: 20))
.fontWeight(.bold)
.padding()
.background(Color.yellow)
.cornerRadius(12)
.overlay(
RoundedRectangle(cornerRadius: 12)
.stroke(lineWidth: 5)
)
}
.sheet(isPresented: $isPresentingScanner) {
ZStack{
CodeScannerView(codeTypes: [.qr]) { response in
if case let .success(result) = response {
scannedCode = result.string
isPresentingScanner = false
}
}
QRCodeGuideLineView()
}
}
Spacer().frame(height:30)
}
}
}
}
struct QRCodeScannerExampleView_Previews: PreviewProvider {
static var previews: some View {
QRCodeScannerExampleView()
}
}
#3_3. MyWebViewimport SwiftUI
import WebKit
//uikit의 uiview를 사용할 수 있도록한다.
//UIViewControllerRepresentable
struct MyWebView: UIViewRepresentable{
var urlToLoad: String
//UI View만들기
func makeUIView(context: Context) -> WKWebView {
//unwrapping
guard let url = URL(string: self.urlToLoad) else {
return WKWebView() //값없을때 리턴
}
//웹뷰 인스턴스 생서
let webview = WKWebView()
//웹뷰 로드
webview.load(URLRequest(url: url))
return webview
}
//업데이트 UI View
func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<MyWebView>) {
}
}
struct MyWebView_Previews: PreviewProvider {
static var previews: some View {
MyWebView(urlToLoad: "https://www.naver.com")
}
}
'SwiftUI 앱만들기' 카테고리의 다른 글
#17. 텍스트필드(TextField, SecureField) (0) 2022.07.11 #16. 버튼 스타일(Button Style) (0) 2022.07.11 #14. 커스텀 탭뷰(CustomTabView) (0) 2022.07.08 #13. 탭뷰(TabView) (0) 2022.07.07 #12-1. 지오메트리 프록시 위치 잡는 3가지 방법 (0) 2022.07.07