Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: QRCode reader for deep links - WPB-14547 #2187

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
4 changes: 4 additions & 0 deletions wire-ios/Wire-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
01BC68652CE496A500445243 /* EmptyPlaceholderContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01BC68642CE496A500445243 /* EmptyPlaceholderContainerView.swift */; };
01C1A7C72A54C45A0058D578 /* SnapshotTesting in Frameworks */ = {isa = PBXBuildFile; productRef = 01C1A7C62A54C45A0058D578 /* SnapshotTesting */; };
01C488B02C8A45F80066789E /* WireAnalytics_DatadogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01C488AF2C8A45F80066789E /* WireAnalytics_DatadogTests.swift */; };
01D2C78F2CECC41D00F05E5E /* QRCodeScannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D2C78E2CECC41D00F05E5E /* QRCodeScannerViewController.swift */; };
01D8E71A2BA39CE900B71CB7 /* PrivacyWarningChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01D8E7192BA39CE900B71CB7 /* PrivacyWarningChecker.swift */; };
01F5EAE42B712DD7009FD25D /* LogFileDestination.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01F5EAE32B712DD6009FD25D /* LogFileDestination.swift */; };
06024ABC29E69A860032CC31 /* ConversationMessageFailedRecipientsCellDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 06024ABB29E69A850032CC31 /* ConversationMessageFailedRecipientsCellDescription.swift */; };
Expand Down Expand Up @@ -2015,6 +2016,7 @@
01BC68502CE3AF9E00445243 /* EmptyConversationSearchResultsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyConversationSearchResultsView.swift; sourceTree = "<group>"; };
01BC68642CE496A500445243 /* EmptyPlaceholderContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyPlaceholderContainerView.swift; sourceTree = "<group>"; };
01C488AF2C8A45F80066789E /* WireAnalytics_DatadogTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireAnalytics_DatadogTests.swift; sourceTree = "<group>"; };
01D2C78E2CECC41D00F05E5E /* QRCodeScannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeScannerViewController.swift; sourceTree = "<group>"; };
01D8E7192BA39CE900B71CB7 /* PrivacyWarningChecker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyWarningChecker.swift; sourceTree = "<group>"; };
01F5EAE32B712DD6009FD25D /* LogFileDestination.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogFileDestination.swift; sourceTree = "<group>"; };
06024ABB29E69A850032CC31 /* ConversationMessageFailedRecipientsCellDescription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMessageFailedRecipientsCellDescription.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -7955,6 +7957,7 @@
children = (
EE593F9B2B73C3F0008D1109 /* DeepLinksView.swift */,
EE593F992B73C3E6008D1109 /* DeepLinksViewModel.swift */,
01D2C78E2CECC41D00F05E5E /* QRCodeScannerViewController.swift */,
);
path = DeepLinks;
sourceTree = "<group>";
Expand Down Expand Up @@ -10256,6 +10259,7 @@
1639A85D2265FC8800868AB9 /* UIAlertController+Availability.swift in Sources */,
16D74BEE2B5933D000160298 /* AddUsernameStepDescription.swift in Sources */,
BFE57B4B1D52335A00CB0806 /* ConversationPreviewViewController.swift in Sources */,
01D2C78F2CECC41D00F05E5E /* QRCodeScannerViewController.swift in Sources */,
5E628021221ED0A60039A8AB /* ProfileActionsFactory.swift in Sources */,
EF2D6D43228C741500D8DBF4 /* PhotoPermissionsControllerStrategy.swift in Sources */,
5EBF4A2B21B958F30021CFFC /* MessageToolboxDataSource.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ struct DeepLinksView: View {
// MARK: - Views

var body: some View {
List {
Section("Open deeplink") {
TextField("Link", text: $urlString, prompt: Text("Enter deeplink"))
Button("Go") {
viewModel.openLink(urlString: urlString)
VStack(spacing: 0) {
List {
Section("Open deeplink") {
TextField("Link", text: $urlString, prompt: Text("Enter deeplink"))
Button("Go") {
viewModel.openLink(urlString: urlString)
}
}
.disabled(urlString.isEmpty)
}
.listStyle(InsetGroupedListStyle())
.frame(height: 150)

QRCodeScannerView { scannedCode in
urlString = scannedCode
viewModel.openLink(urlString: scannedCode)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)

}
.alert(
isPresented: $viewModel.isShowingAlert,
Expand All @@ -46,6 +57,21 @@ struct DeepLinksView: View {
}
}

struct QRCodeScannerView: UIViewControllerRepresentable {

var onQRCodeScanned: (String) -> Void

func makeUIViewController(context: Context) -> QRCodeScannerViewController {
let viewController = QRCodeScannerViewController()
viewController.onQRCodeScanned = onQRCodeScanned
return viewController
}

func updateUIViewController(_ uiViewController: QRCodeScannerViewController, context: Context) {
// Nothing to update here.
}
}

// MARK: - Previews

#Preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class DeepLinksViewModel: ObservableObject {

func openLink(urlString: String) {
guard
let url = URL(string: urlString),
let url = URL(string: urlString.trim()),
(try? URLAction(url: url)) != nil
else {
error = .invalidLink
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Wire
// Copyright (C) 2024 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//

import AVFoundation
import UIKit

final class QRCodeScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

private var captureSession: AVCaptureSession!
private var previewLayer: AVCaptureVideoPreviewLayer!
var onQRCodeScanned: ((String) -> Void)?

override func viewDidLoad() {
super.viewDidLoad()

captureSession = AVCaptureSession()

guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else {
return
}

let videoInput: AVCaptureDeviceInput

do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}

if captureSession.canAddInput(videoInput) {
captureSession.addInput(videoInput)
} else {
failed()
return
}

let metadataOutput = AVCaptureMetadataOutput()

if captureSession.canAddOutput(metadataOutput) {
captureSession.addOutput(metadataOutput)

metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.qr]
} else {
failed()
return
}

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)

captureSession.startRunning()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-[AVCaptureSession startRunning] should be called from background thread. Calling it on the main thread can lead to UI unresponsiveness

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added:

DispatchQueue.global(qos: .userInitiated).async {
            self.captureSession.startRunning()
        }

}

private func failed() {
let alertController = UIAlertController(
title: "Scanning not supported",
message: "Your device doesn't support QR code scanning.",
preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
present(alertController, animated: true)
captureSession = nil
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

if captureSession.isRunning {
captureSession.stopRunning()
}
}

func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
captureSession.stopRunning()

if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
onQRCodeScanned?(stringValue)
}
}

override var prefersStatusBarHidden: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
Loading