YagSill 2021. 12. 31. 15:47
728x90

QR코드로 뭔가를 하기 위해서는 일단 구글에 검색을 해야합니다. 

왜냐면 혼자서 이 많은 코드를 다 짜고 있을 순 없거등요

 

https://github.com/TuenTuenna/qrcode_reader_swift_tutorial/tree/master/Pods/QRCodeReader.swift

 

GitHub - TuenTuenna/qrcode_reader_swift_tutorial

Contribute to TuenTuenna/qrcode_reader_swift_tutorial development by creating an account on GitHub.

github.com

요기 보시면 사용방법 다 나와있습니다.

 

일단

 

1) pod 추가

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target 'TargetName' do
    pod 'QRCodeReader.swift', '~> 10.1.0'
end

 

여기서 마지막 윗줄

pod 'QRCodeReader.swift', '~> 10.1.0'

요거를 podfile에 추가를 하시면 됩니다.

 

이후에 라이브러리를 import 하시면 되요

import AVFoundation
import QRCodeReader

-> AVFoundation도 import 해주셔야 합니다. 요 라이브러리는 미디어에 관련되어있는 것들을 사용할 수 있는 좋은 도구이기 때문에 이것도 같이 import 시켜줍니다.

 

2. QRCodeReaderViewControllerDelegate 상속받기

class WebVIewViewController: UIViewController, QRCodeReaderViewControllerDelegate

QRCodeReaderViewControllerDelegate 를 상속받으면 이 Delegate가 갖고있는 메소드를 정의해 주어야 합니다.

 func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
        reader.stopScanning()
        dismiss(animated: true, completion: nil)
    }
    
    func readerDidCancel(_ reader: QRCodeReaderViewController) {
        reader.stopScanning()
        dismiss(animated: true, completion: nil)
    }

설명하자면

func reader -> 이 함수는 스캔이 되었을 때 어떻게 할꺼냐? 라는 함수이고

func readerDidCancel -> 이 함수는 스캔에 실패하였을 때 어떻게 할것이냐? 에 대한 함수입니다.

 

3. QRCode 실행 함수 정의

//QR코드 리더 뷰 컨트롤러 만든다
    lazy var readerVC: QRCodeReaderViewController = {
        let builder = QRCodeReaderViewControllerBuilder {
            $0.reader = QRCodeReader(metadataObjectTypes: [.qr], captureDevicePosition: .back)
            
            // Configure the view controller (optional)
            $0.showTorchButton        = false
            $0.showSwitchCameraButton = false
            $0.showCancelButton       = false
            $0.showOverlayView        = true
            $0.rectOfInterest         = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
        }
        
        return QRCodeReaderViewController(builder: builder)
    }()

일단 QR코드 리더 뷰 컨트롤러를 만들어 줄겁니다.

@objc func QRcodeLaunch(){
        print("MainViewController - QRcodeLaunch called")
        
        readerVC.delegate = self

          // Or by using the closure pattern
          readerVC.completionBlock = { (result: QRCodeReaderResult?) in
            print(result)
            guard let sacnnedUrlString = result?.value else { return }
            print("scanningQRCode : \(sacnnedUrlString)")
            
            let scanned = URL(string: sacnnedUrlString)
            self.WebView.load(URLRequest(url : scanned!))
            
          }
          // Presents the readerVC as modal form sheet
          readerVC.modalPresentationStyle = .formSheet
          present(readerVC, animated: true, completion: nil)
    }

그리고 QR코드의 함수를 정의해주면 됩니다...

 

자세한건 아래 "개발하는 정대리"님 유튜브를 참고하시면 됩니다.

 

https://www.youtube.com/watch?v=Q5slh29MZ_I&list=PLgOlaPUIbynqRzpQBIdEDnjDdkVsjHqxK&index=5 

 

728x90