diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51ebfc7..889907f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,14 +12,14 @@ jobs: runs-on: macOS-latest strategy: matrix: - destination: ['platform=iOS Simulator,OS=16.1,name=iPhone 14 Pro Max'] - xcode: ['/Applications/Xcode_14.1.app/Contents/Developer'] + destination: ['platform=iOS Simulator,OS=18.0,name=iPhone 16 Pro Max'] steps: - name: Checkout - uses: actions/checkout@v1 - - name: Run test - run: xcodebuild test -workspace QRScanner.xcworkspace -scheme UnitTests -destination "${destination}" + uses: actions/checkout@v4 + - name: Select latest Xcode + run: sudo xcode-select -s /Applications/Xcode.app/Contents/Developer + - name: Run unit tests + run: xcodebuild test -workspace QRScanner.xcworkspace -scheme UnitTests -destination "${destination}" CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO env: destination: ${{ matrix.destination }} - DEVELOPER_DIR: ${{ matrix.xcode }} diff --git a/MercariQRScanner.podspec b/MercariQRScanner.podspec index b443c1d..e795c99 100644 --- a/MercariQRScanner.podspec +++ b/MercariQRScanner.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.license = "MIT" s.author = { "hitsubunnu" => "idhitsu@gmail.com" } s.swift_version = "5.0" - s.ios.deployment_target = "10.0" + s.ios.deployment_target = "14.0" s.source = { :git => "https://github.com/mercari/QRScanner.git", :tag => s.version } s.source_files = "QRScanner/*.swift", "QRScanner/**/*.swift" s.resources = "QRScanner/*.xcassets" diff --git a/Package.swift b/Package.swift index 2bfeddc..d9af112 100644 --- a/Package.swift +++ b/Package.swift @@ -5,7 +5,7 @@ import PackageDescription let package = Package( name: "QRScanner", platforms: [ - .iOS(.v10), + .iOS(.v14), ], products: [ .library( diff --git a/QRScanner.xcodeproj/project.pbxproj b/QRScanner.xcodeproj/project.pbxproj index 2889e7a..d6bfdde 100644 --- a/QRScanner.xcodeproj/project.pbxproj +++ b/QRScanner.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 16D0D5EE23570BC800A2EC78 /* QRScanner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16D0D5E423570BC800A2EC78 /* QRScanner.framework */; }; 16D0D5F523570BC800A2EC78 /* QRScanner.h in Headers */ = {isa = PBXBuildFile; fileRef = 16D0D5E723570BC800A2EC78 /* QRScanner.h */; settings = {ATTRIBUTES = (Public, ); }; }; 43820BFF256A29DD0046AC78 /* Bundle+Module.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43820BFE256A29DD0046AC78 /* Bundle+Module.swift */; }; + 43820C00256A29DD0046AC79 /* QRScannerSwiftUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43820BFF256A29DD0046AC79 /* QRScannerSwiftUIView.swift */; }; 76124403238BCEA70020466C /* QRScannerViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76124402238BCEA70020466C /* QRScannerViewTests.swift */; }; 76E80674238BB26E0000C667 /* QRScannerSpyDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76E80673238BB26E0000C667 /* QRScannerSpyDelegate.swift */; }; /* End PBXBuildFile section */ @@ -37,6 +38,7 @@ 16D0D5ED23570BC800A2EC78 /* QRScannerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = QRScannerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 16D0D5F423570BC800A2EC78 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 43820BFE256A29DD0046AC78 /* Bundle+Module.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bundle+Module.swift"; sourceTree = ""; }; + 43820BFF256A29DD0046AC79 /* QRScannerSwiftUIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "QRScannerSwiftUIView.swift"; sourceTree = ""; }; 76124402238BCEA70020466C /* QRScannerViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScannerViewTests.swift; sourceTree = ""; }; 76E80673238BB26E0000C667 /* QRScannerSpyDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRScannerSpyDelegate.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -84,6 +86,7 @@ 16D0D5E723570BC800A2EC78 /* QRScanner.h */, 43820BFE256A29DD0046AC78 /* Bundle+Module.swift */, 16A16402235710D80032684C /* QRScannerView.swift */, + 43820BFF256A29DD0046AC79 /* QRScannerSwiftUIView.swift */, 16A16404235711240032684C /* QRScannerError.swift */, 16D0D5E823570BC800A2EC78 /* Info.plist */, 16A16406235712360032684C /* Images.xcassets */, @@ -231,6 +234,7 @@ 16A16405235711240032684C /* QRScannerError.swift in Sources */, 16A16403235710D80032684C /* QRScannerView.swift in Sources */, 43820BFF256A29DD0046AC78 /* Bundle+Module.swift in Sources */, + 43820C00256A29DD0046AC79 /* QRScannerSwiftUIView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -306,7 +310,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -364,7 +368,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = iphoneos; diff --git a/QRScanner.xcworkspace/contents.xcworkspacedata b/QRScanner.xcworkspace/contents.xcworkspacedata index 7a0f0ce..75a5519 100644 --- a/QRScanner.xcworkspace/contents.xcworkspacedata +++ b/QRScanner.xcworkspace/contents.xcworkspacedata @@ -48,4 +48,7 @@ + + diff --git a/QRScanner/QRScannerSwiftUIView.swift b/QRScanner/QRScannerSwiftUIView.swift new file mode 100644 index 0000000..f5f7f87 --- /dev/null +++ b/QRScanner/QRScannerSwiftUIView.swift @@ -0,0 +1,133 @@ +import SwiftUI + +// MARK: - QRScannerSwiftUIView +public struct QRScannerSwiftUIView: UIViewRepresentable { + + // MARK: - Configuration + public struct Configuration { + public let focusImage: UIImage? + public let focusImagePadding: CGFloat + public let animationDuration: Double + public let isBlurEffectEnabled: Bool + + public init( + focusImage: UIImage? = nil, + focusImagePadding: CGFloat = 8.0, + animationDuration: Double = 0.5, + isBlurEffectEnabled: Bool = false + ) { + self.focusImage = focusImage + self.focusImagePadding = focusImagePadding + self.animationDuration = animationDuration + self.isBlurEffectEnabled = isBlurEffectEnabled + } + } + + // MARK: - Properties + private let configuration: Configuration + private let onSuccess: (String) -> Void + private let onFailure: (QRScannerError) -> Void + private let onTorchActiveChange: ((Bool) -> Void)? + @Binding private var isScanning: Bool + @Binding private var torchActive: Bool + + // MARK: - Initializers + public init( + configuration: Configuration = Configuration(), + isScanning: Binding = .constant(true), + torchActive: Binding = .constant(false), + onSuccess: @escaping (String) -> Void, + onFailure: @escaping (QRScannerError) -> Void, + onTorchActiveChange: ((Bool) -> Void)? = nil + ) { + self.configuration = configuration + self._isScanning = isScanning + self._torchActive = torchActive + self.onSuccess = onSuccess + self.onFailure = onFailure + self.onTorchActiveChange = onTorchActiveChange + } + + // MARK: - UIViewRepresentable + public func makeUIView(context: Context) -> QRScannerView { + let qrScannerView = QRScannerView() + let input = QRScannerView.Input( + focusImage: configuration.focusImage, + focusImagePadding: configuration.focusImagePadding, + animationDuration: configuration.animationDuration, + isBlurEffectEnabled: configuration.isBlurEffectEnabled + ) + qrScannerView.configure(delegate: context.coordinator, input: input) + context.coordinator.qrScannerView = qrScannerView + + if isScanning { + qrScannerView.startRunning() + } + + return qrScannerView + } + + public func updateUIView(_ uiView: QRScannerView, context: Context) { + if isScanning { + uiView.startRunning() + } else { + uiView.stopRunning() + } + + uiView.setTorchActive(isOn: torchActive) + } + + public func makeCoordinator() -> Coordinator { + Coordinator( + isScanning: $isScanning, + torchActive: $torchActive, + onSuccess: onSuccess, + onFailure: onFailure, + onTorchActiveChange: onTorchActiveChange + ) + } + + // MARK: - Coordinator + public class Coordinator: NSObject, QRScannerViewDelegate { + @Binding private var isScanning: Bool + @Binding private var torchActive: Bool + private let onSuccess: (String) -> Void + private let onFailure: (QRScannerError) -> Void + private let onTorchActiveChange: ((Bool) -> Void)? + + weak var qrScannerView: QRScannerView? + + init( + isScanning: Binding, + torchActive: Binding, + onSuccess: @escaping (String) -> Void, + onFailure: @escaping (QRScannerError) -> Void, + onTorchActiveChange: ((Bool) -> Void)? + ) { + self._isScanning = isScanning + self._torchActive = torchActive + self.onSuccess = onSuccess + self.onFailure = onFailure + self.onTorchActiveChange = onTorchActiveChange + } + + public func qrScannerView(_ qrScannerView: QRScannerView, didSuccess code: String) { + Task { @MainActor in + self.isScanning = false + } + onSuccess(code) + } + + public func qrScannerView(_ qrScannerView: QRScannerView, didFailure error: QRScannerError) { + onFailure(error) + } + + public func qrScannerView(_ qrScannerView: QRScannerView, didChangeTorchActive isOn: Bool) { + Task { @MainActor in + self.torchActive = isOn + } + onTorchActiveChange?(isOn) + } + } +} + diff --git a/QRScanner/QRScannerView.swift b/QRScanner/QRScannerView.swift index 098dce4..2159ab2 100644 --- a/QRScanner/QRScannerView.swift +++ b/QRScanner/QRScannerView.swift @@ -126,6 +126,19 @@ public class QRScannerView: UIView { videoDevice.unlockForConfiguration() } + public override func layoutSubviews() { + super.layoutSubviews() + if previewLayer?.frame != self.bounds { + previewLayer?.frame = self.bounds + blurEffectView.frame = self.bounds + + let width = self.bounds.width * 0.618 + let x = self.bounds.width * 0.191 + let y = self.bounds.height * 0.191 + focusImageView.frame = CGRect(x: x, y: y, width: width, height: width) + } + } + deinit { setTorchActive(isOn: false) focusImageView.removeFromSuperview() diff --git a/QRScannerSample/QRScannerSample.xcodeproj/project.pbxproj b/QRScannerSample/QRScannerSample.xcodeproj/project.pbxproj index 35c2227..9b08a7e 100644 --- a/QRScannerSample/QRScannerSample.xcodeproj/project.pbxproj +++ b/QRScannerSample/QRScannerSample.xcodeproj/project.pbxproj @@ -237,7 +237,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -292,7 +292,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.0; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = iphoneos; diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.pbxproj b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..87160f9 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.pbxproj @@ -0,0 +1,349 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 16A163F4235710050032684C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 16A163F3235710050032684C /* Assets.xcassets */; }; + 16A163F7235710050032684C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 16A163F5235710050032684C /* LaunchScreen.storyboard */; }; + 16A163FF235710580032684C /* QRScanner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 16A163FE235710580032684C /* QRScanner.framework */; }; + 16A16400235710580032684C /* QRScanner.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 16A163FE235710580032684C /* QRScanner.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 16A164A2235710580032684C /* QRScannerSwiftUIApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A164A0235710580032684C /* QRScannerSwiftUIApp.swift */; }; + 16A164A3235710580032684C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A164A1235710580032684C /* ContentView.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 16A16401235710580032684C /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 16A16400235710580032684C /* QRScanner.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 16A163E9235710040032684C /* QRScannerSwiftUISample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QRScannerSwiftUISample.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 16A163F3235710050032684C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + 16A163F6235710050032684C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + 16A163F8235710050032684C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 16A163FE235710580032684C /* QRScanner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = QRScanner.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 16A164A0235710580032684C /* QRScannerSwiftUIApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRScannerSwiftUIApp.swift; sourceTree = ""; }; + 16A164A1235710580032684C /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 16A163E6235710040032684C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 16A163FF235710580032684C /* QRScanner.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 16A163E0235710040032684C = { + isa = PBXGroup; + children = ( + 16A163FE235710580032684C /* QRScanner.framework */, + 16A163EB235710040032684C /* QRScannerSwiftUISample */, + 16A163EA235710040032684C /* Products */, + ); + sourceTree = ""; + }; + 16A163EA235710040032684C /* Products */ = { + isa = PBXGroup; + children = ( + 16A163E9235710040032684C /* QRScannerSwiftUISample.app */, + ); + name = Products; + sourceTree = ""; + }; + 16A163EB235710040032684C /* QRScannerSwiftUISample */ = { + isa = PBXGroup; + children = ( + 16A164A0235710580032684C /* QRScannerSwiftUIApp.swift */, + 16A164A1235710580032684C /* ContentView.swift */, + 16A163F3235710050032684C /* Assets.xcassets */, + 16A163F5235710050032684C /* LaunchScreen.storyboard */, + 16A163F8235710050032684C /* Info.plist */, + ); + path = QRScannerSwiftUISample; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 16A163E8235710040032684C /* QRScannerSwiftUISample */ = { + isa = PBXNativeTarget; + buildConfigurationList = 16A163FB235710050032684C /* Build configuration list for PBXNativeTarget "QRScannerSwiftUISample" */; + buildPhases = ( + 16A163E5235710040032684C /* Sources */, + 16A163E6235710040032684C /* Frameworks */, + 16A163E7235710040032684C /* Resources */, + 16A16401235710580032684C /* Embed Frameworks */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = QRScannerSwiftUISample; + productName = QRScannerSwiftUISample; + productReference = 16A163E9235710040032684C /* QRScannerSwiftUISample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 16A163E1235710040032684C /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 1030; + LastUpgradeCheck = 1030; + ORGANIZATIONNAME = mercari.com; + TargetAttributes = { + 16A163E8235710040032684C = { + CreatedOnToolsVersion = 10.3; + }; + }; + }; + buildConfigurationList = 16A163E4235710040032684C /* Build configuration list for PBXProject "QRScannerSwiftUISample" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 16A163E0235710040032684C; + productRefGroup = 16A163EA235710040032684C /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 16A163E8235710040032684C /* QRScannerSwiftUISample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 16A163E7235710040032684C /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 16A163F7235710050032684C /* LaunchScreen.storyboard in Resources */, + 16A163F4235710050032684C /* Assets.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 16A163E5235710040032684C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 16A164A2235710580032684C /* QRScannerSwiftUIApp.swift in Sources */, + 16A164A3235710580032684C /* ContentView.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + 16A163F5235710050032684C /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + 16A163F6235710050032684C /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + 16A163F9235710050032684C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 16A163FA235710050032684C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 14.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 16A163FC235710050032684C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = K69VE4UMZN; + INFOPLIST_FILE = QRScannerSwiftUISample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.mercari.QRScannerSwiftUISample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 16A163FD235710050032684C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = K69VE4UMZN; + INFOPLIST_FILE = QRScannerSwiftUISample/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.mercari.QRScannerSwiftUISample; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 16A163E4235710040032684C /* Build configuration list for PBXProject "QRScannerSwiftUISample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 16A163F9235710050032684C /* Debug */, + 16A163FA235710050032684C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 16A163FB235710050032684C /* Build configuration list for PBXNativeTarget "QRScannerSwiftUISample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 16A163FC235710050032684C /* Debug */, + 16A163FD235710050032684C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 16A163E1235710040032684C /* Project object */; +} diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..2cf3fba --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/xcshareddata/xcschemes/QRScannerSwiftUISample.xcscheme b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/xcshareddata/xcschemes/QRScannerSwiftUISample.xcscheme new file mode 100644 index 0000000..8b42d46 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample.xcodeproj/xcshareddata/xcschemes/QRScannerSwiftUISample.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/AppIcon.appiconset/Contents.json b/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..d8db8d6 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,98 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/Contents.json b/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/Base.lproj/LaunchScreen.storyboard b/QRScannerSwiftUISample/QRScannerSwiftUISample/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..dc2947b --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/ContentView.swift b/QRScannerSwiftUISample/QRScannerSwiftUISample/ContentView.swift new file mode 100644 index 0000000..00200e4 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/ContentView.swift @@ -0,0 +1,77 @@ +import SwiftUI +import QRScanner +import AVFoundation + +struct ContentView: View { + @State private var isScanning = true + @State private var isTorchOn = false + + var body: some View { + ZStack { + QRScannerSwiftUIView( + configuration: .init(isBlurEffectEnabled: true), + isScanning: $isScanning, + torchActive: $isTorchOn, + onSuccess: { qrCode in + print("QR Code scanned: \(qrCode)") + }, + onFailure: { error in + print("QR Scanner error: \(error.localizedDescription)") + }, + onTorchActiveChange: { isOn in + isTorchOn = isOn + } + ) + .edgesIgnoringSafeArea(.all) + .onAppear { + setupQRScanner() + } + .onDisappear { + isScanning = false + } + + VStack { + Spacer() + HStack { + Spacer() + + Button(action: { + isTorchOn.toggle() + }) { + Image(systemName: isTorchOn ? "flashlight.on.fill" : "flashlight.off.fill") + .font(.title2) + .foregroundColor(.white) + .frame(width: 44, height: 44) + .background(Color.black.opacity(0.6)) + .clipShape(Circle()) + } + .padding(.trailing, 20) + .padding(.bottom, 50) + } + } + } + } + + private func setupQRScanner() { + switch AVCaptureDevice.authorizationStatus(for: .video) { + case .authorized: + isScanning = true + case .notDetermined: + AVCaptureDevice.requestAccess(for: .video) { granted in + DispatchQueue.main.async { + if granted { + isScanning = true + } else { + print("Camera is required to use in this application") + } + } + } + default: + print("Camera access denied or restricted") + } + } +} + +#Preview { + ContentView() +} diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/Info.plist b/QRScannerSwiftUISample/QRScannerSwiftUISample/Info.plist new file mode 100644 index 0000000..378668a --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/Info.plist @@ -0,0 +1,43 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + NSCameraUsageDescription + Camera is required to use this application + + diff --git a/QRScannerSwiftUISample/QRScannerSwiftUISample/QRScannerSwiftUIApp.swift b/QRScannerSwiftUISample/QRScannerSwiftUISample/QRScannerSwiftUIApp.swift new file mode 100644 index 0000000..00863b9 --- /dev/null +++ b/QRScannerSwiftUISample/QRScannerSwiftUISample/QRScannerSwiftUIApp.swift @@ -0,0 +1,10 @@ +import SwiftUI + +@main +struct QRScannerSwiftUIApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index 1bb4dec..6c64ab5 100644 --- a/README.md +++ b/README.md @@ -1,81 +1,183 @@ # QRScanner -A simple QR Code scanner framework for iOS. Provides a similar scan effect to ios13+. Written in Swift. +A modern QR Code scanner framework for iOS with comprehensive SwiftUI and UIKit support. Delivers a native iOS scanning experience with advanced customization options. Written in Swift. * [日本語のブログ](https://tech.mercari.com/entry/2019/12/12/094129) -|iOS 13.0+| Use QRScanner in iOS 10.0+| +|iPhone native camera|QRScanner implementation| |-|-| ||| "QR Code" is a registered trademark of DENSO WAVE INCORPORATED -## Feature -- Similar to iOS 13.0+ design -- Simple usage Sample -- Support from iOS 10.0+ +## Features -## Development Requirements -- iOS 11.0+ -- Swift: 5.7.1 -- Xcode Version: 14.1 +- 🎯 **Native iOS Design** - Matches iPhone's built-in camera scanning experience +- 🚀 **SwiftUI & UIKit Support** - Full compatibility with both modern and traditional iOS development +- 📱 **iOS 14.0+** - Built for modern iOS with latest Swift features +- ⚡ **Easy Integration** - Simple setup with comprehensive examples +- 🎨 **Highly Customizable** - Extensive configuration options for focus frame, animations, and effects +- 💡 **Production Ready** - Battle-tested in Mercari's production apps -## Installation -QRScanner supports multiple methods for installing the library in a project. +**Quick Start:** [SwiftUI Example](https://github.com/mercari/QRScanner/tree/master/QRScannerSwiftUISample) | [UIKit Example](https://github.com/mercari/QRScanner/tree/master/QRScannerSample) -### Installation with CocoaPods +## Requirements -- To integrate QRScanner into your Xcode project using CocoaPods, specify it in your Podfile -```ruby - platform :ios, '11.0' - pod 'MercariQRScanner' -``` +- **iOS:** 14.0+ +- **Swift:** 5.9+ +- **Xcode:** 16.0+ -- Run command -``` - pod install -``` -- Write Import statement on your source file -```swift - import MercariQRScanner -``` +## Installation -### Installation with Swift Package Manager +### Swift Package Manager (Recommended) -Once you have your Swift package set up, adding QRScanner as a dependency is as easy as adding it to the dependencies value of your Package.swift. -``` +#### Via Xcode (Easiest) +1. **File** → **Add Package Dependencies** +2. Enter: `https://github.com/mercari/QRScanner.git` +3. Select version and add to your target + +#### Via Package.swift +```swift dependencies: [ .package(url: "https://github.com/mercari/QRScanner.git", .upToNextMajor(from: "1.9.0")) ] ``` -- Write Import statement on your source file +#### Import ```swift import QRScanner ``` -### Installation with Carthage +## Quick Setup -- To integrate QRScanner, add the following to your Cartfile. -``` -github "mercari/QRScanner" +### 1. Camera Permission +Add camera usage description to your `Info.plist`: + +```xml +NSCameraUsageDescription +Camera access is required for QR code scanning ``` -- Write Import statement on your source file + + + +### 2. Choose Your Framework +- **SwiftUI**: Modern declarative UI (recommended for new projects) +- **UIKit**: Traditional imperative UI (for existing projects) + +Complete examples: [SwiftUI Sample](https://github.com/mercari/QRScanner/tree/master/QRScannerSwiftUISample) | [UIKit Sample](https://github.com/mercari/QRScanner/tree/master/QRScannerSample) + +## SwiftUI Usage + +> 💡 **Recommended for new projects** - SwiftUI provides a more modern and concise API + +### Basic Implementation + ```swift +import SwiftUI import QRScanner +import AVFoundation + +struct ContentView: View { + @State private var scannedCode = "" + @State private var isPresented = false + + var body: some View { + VStack { + Button("Start Scanning") { + isPresented = true + } + + if !scannedCode.isEmpty { + Text("Scanned: \(scannedCode)") + } + } + .sheet(isPresented: $isPresented) { + QRScannerSwiftUIView( + onSuccess: { code in + scannedCode = code + isPresented = false + }, + onFailure: { error in + print("Scan error: \(error)") + isPresented = false + } + ) + } + } +} +``` + +### Advanced Implementation + +```swift +struct AdvancedQRScannerView: View { + @State private var scannedCode = "" + @State private var isScanning = true + @State private var torchActive = false + + var body: some View { + VStack { + QRScannerSwiftUIView( + configuration: .init( + focusImagePadding: 12.0, + animationDuration: 0.3, + isBlurEffectEnabled: true + ), + isScanning: $isScanning, + torchActive: $torchActive, + onSuccess: { code in + scannedCode = code + isScanning = false + }, + onFailure: { error in + print("Error: \(error.localizedDescription)") + } + ) + + HStack { + Button(isScanning ? "Pause" : "Resume") { + isScanning.toggle() + } + + Button("Torch") { + torchActive.toggle() + } + } + .padding() + } + } +} ``` -## Usage +### Configuration Options -See [QRScannerSample](https://github.com/mercari/QRScanner/tree/master/QRScannerSample) +| Parameter | Type | Default | Description | +|-----------|------|---------|-------------| +| `focusImage` | `UIImage?` | `nil` | Custom focus frame image | +| `focusImagePadding` | `CGFloat` | `8.0` | Focus frame padding | +| `animationDuration` | `Double` | `0.5` | Animation duration | +| `isBlurEffectEnabled` | `Bool` | `false` | Enable blur effect | -### Add `Privacy - Camera Usage Description` to Info.plist file +| Binding | Type | Description | +|---------|------|-------------| +| `isScanning` | `Bool` | Control scanning state | +| `torchActive` | `Bool` | Control torch state | - +| Callback | Description | +|----------|-------------| +| `onSuccess` | Called when QR code is successfully scanned | +| `onFailure` | Called when scanning fails | +| `onTorchActiveChange` | Called when torch state changes | + +📘 **Complete Example:** [QRScannerSwiftUISample](https://github.com/mercari/QRScanner/tree/master/QRScannerSwiftUISample) + +## UIKit Usage -### The Basis Of Usage +> 🔧 **For existing projects** - UIKit integration with full customization support + +### Basic Implementation ```swift -import QRScanner // If use the Pod way, please import MercariQRScanner +import QRScanner import AVFoundation final class ViewController: UIViewController { @@ -130,8 +232,6 @@ extension ViewController: QRScannerViewDelegate { ### Customization -#### Source Code Way - ```swift override func viewDidLoad() { super.viewDidLoad() @@ -155,9 +255,9 @@ override func viewDidLoad() { |-|-| ||| -### Add FlashButton +### Adding Flash Control -[FlashButtonSample](https://github.com/mercari/QRScanner/blob/master/QRScannerSample/QRScannerSample/FlashButton.swift) +Example: [FlashButton.swift](https://github.com/mercari/QRScanner/blob/master/QRScannerSample/QRScannerSample/FlashButton.swift) ```swift final class ViewController: UIViewController { @@ -181,19 +281,13 @@ extension ViewController: QRScannerViewDelegate { } ``` -### Add Blur Effect - -#### Source Code Way +### Adding Blur Effect ```swift - qrScannerView.configure(delegate: self, input: .init(isBlurEffectEnabled: true)) +qrScannerView.configure(delegate: self, input: .init(isBlurEffectEnabled: true)) ``` -#### Interface Builder Way - -|Customize| -|-| -|| +📘 **Complete Example:** [QRScannerSample](https://github.com/mercari/QRScanner/tree/master/QRScannerSample) ## Committers